Home > database >  How do I get R to recognize the appropriate order for Month Year combinations without manually speci
How do I get R to recognize the appropriate order for Month Year combinations without manually speci

Time:03-17

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

#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

CodePudding user response:

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()
  • Related