I want to create a ggplot chart for this dataset:
head(df3)
pedestrians motorist cyclist
1 1377 1100 200
And I did this:
ggplot(df3, aes(x = 1:3, y = c(pedestrians, motorist, cyclist)))
geom_col()
ggtitle("Accidents by Mode of Transportation")
scale_x_discrete(labels = c("Pedestrians", "Motorists", "Cyclists"))
And got this output:
Error in `geom_col()`:
! Problem while computing aesthetics.
ℹ Error occurred in the 1st layer.
Caused by error in `check_aesthetics()`:
! Aesthetics must be either length 1 or the same as the data (1)
✖ Fix the following mappings: `x` and `y`
Run `rlang::last_error()` to see where the error occurred.
CodePudding user response:
Your data is not yet in tidy format. Below you see my approach to this:
library(tidyverse)
df3 <- data.frame(
types= c("pedestrians","motorists", "cyclists"),
accidents= c(1377,1100,200)
)
ggplot(df3, aes(x=types, y=accidents))
geom_col()