Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I create a bar chart grouping the frequency of months present in a column?

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading