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 do I get R to recognize the appropriate order for Month Year combinations without manually specifying the order?

I have a list of dates, and I need to report them by month and year (Mar 2020, Apr 2020, etc). However, when I parse the Month and Year from the date, I get a character string instead of a date, so when I try to plot it into ggplot, the order is alphabetical instead of chronological.

I know I can manually specify an order with factor, but typing out every month and year combination will be painful–is there a more efficient way to tackle this problem? I tried wrapping my date in my() from lubridate, but that didn’t work.

#My sample data
library(dplyr)

test <- tibble(date = seq(ymd('2021-01-01'), ymd('2021-12-31'), by = "1 day"),
                              values = c(1001:1182, 800:900, 1:82),
                              month = cut.Date(date, breaks = "1 month", labels = FALSE)) %>%
  group_by(month) %>%
  mutate(month = format(last(date), '%b %Y')) %>%
  ungroup()

Here’s a simple plot showing that the order is alphabetical instead of chronological

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

#Simple plot showing that the order is alphabetical instead of chronological

library(ggplot2)
ggplot(test, aes(x = month, y = values)) +
  geom_col()

enter image description here

>Solution :

The reorder function (stats package) can be used to sort factor levels. Here you can use my in the second argument to determine the sort order. So I believe this does what you need:

ggplot(test, aes(x = reorder(month, my(month)), y = values)) + geom_col()
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