I have a CSV file imported into R as a dataset named bachelors
. It has a column I have formatted with as.Date
. bachelors$Posted.On looks like this (minimally reproducible)
Posted.On |
---|
2022-05-18 |
2022-07-14 |
2022-07-22 |
I would like to make a bar chart using the ggplot2 library to plot how many times each month of the year appears in the rows in the column. In the example above, months May and July appear once each, so I want a bar plot to display 2 bars reading '1' for May and '2' for July. The format of the labels don't matter.
I have tried ggplot(bachelors, aes(x = Posted.On, y=count(months(Posted.On)))) geom_bar(stat='identity')
but it throws an error in 1st layer.
CodePudding user response:
You can do
library(ggplot2)
ggplot(bachelors, aes(factor(months(as.Date(Posted.On), TRUE), month.name)))
geom_bar()
labs(x = "Month")
Created on 2022-12-03 with reprex v2.0.2
Question data in reproducible format
bachelors <- structure(list(Posted.On = c("2022-05-18", "2022-07-14",
"2022-07-22")), class = "data.frame", row.names = c(NA, -3L))