rgganimate

How to reformat date in frame_along (gganimate package in R)


I have the following dataframe in R

chart_data <- structure(list(Month = structure(c(19358, 19389, 19417, 19448, 
19478, 19509, 19539, 19570, 19601, 19631, 19662, 19692, 19358, 
19389, 19417, 19448, 19478, 19509, 19539, 19570, 19601, 19631, 
19662, 19692, 19358, 19389, 19417, 19448, 19478, 19509, 19539, 
19570, 19601, 19631, 19662, 19692), class = "Date"), Q25 = c(39.22, 
39.8, 39.32, 39.12, 38.31, 38.38, 37.47, 37.35, 38.04, 38.53, 
38.97, 39.16, 39.22, 39.8, 39.32, 39.12, 38.31, 38.38, 37.47, 
37.35, 38.04, 38.53, 38.97, 39.16, 39.22, 39.8, 39.32, 39.12, 
38.31, 38.38, 37.47, 37.35, 38.04, 38.53, 38.97, 39.16), Q75 = c(41.53, 
41.48, 41.57, 41.34, 41.25, 41.69, 42.12, 41.98, 42.32, 42.12, 
42.47, 42.53, 41.53, 41.48, 41.57, 41.34, 41.25, 41.69, 42.12, 
41.98, 42.32, 42.12, 42.47, 42.53, 41.53, 41.48, 41.57, 41.34, 
41.25, 41.69, 42.12, 41.98, 42.32, 42.12, 42.47, 42.53), Key = c("Average Production (2015-2023)", 
"Average Production (2015-2023)", "Average Production (2015-2023)", 
"Average Production (2015-2023)", "Average Production (2015-2023)", 
"Average Production (2015-2023)", "Average Production (2015-2023)", 
"Average Production (2015-2023)", "Average Production (2015-2023)", 
"Average Production (2015-2023)", "Average Production (2015-2023)", 
"Average Production (2015-2023)", "2022 Production", "2022 Production", 
"2022 Production", "2022 Production", "2022 Production", "2022 Production", 
"2022 Production", "2022 Production", "2022 Production", "2022 Production", 
"2022 Production", "2022 Production", "2023 Production", "2023 Production", 
"2023 Production", "2023 Production", "2023 Production", "2023 Production", 
"2023 Production", "2023 Production", "2023 Production", "2023 Production", 
"2023 Production", "2023 Production"), Value = c(40.4666666666667, 
40.4111111111111, 40.3911111111111, 40.4966666666667, 39.5888888888889, 
39.7211111111111, 39.87, 39.9233333333333, 40.0911111111111, 
40.2577777777778, 40.4166666666667, 40.3622222222222, 39.22, 
39.8, 39.32, 38.83, 38.82, 39.33, 39.86, 40.06, 40.28, 39.78, 
39.72, 39.84, 39.17, 39.44, 39.27, 39.12, 38.31, 38.38, 37.41, 
36.93, 37.66, 37.53, 37.61, 37.5)), row.names = c(NA, -36L), class = c("tbl_df", 
"tbl", "data.frame"))

I'm using this dataframe to create an animated chart using gganimate. I would like to reformat the dates in the subtitle to only display the month (as opposed to the full date). Is there any way to do this?

library(tidyverse)
library(gganimate)

ggplot(chart_data, aes(x = Month, y = Value, col = Key, group = Key, linetype = Key)) +
  geom_ribbon(aes(x = Month, ymin = `Q25`, ymax = `Q75`), fill="#DABCB4", col = 'white', alpha = 0.5) +
  geom_line(linewidth = 3) +
  scale_color_manual(values = c('#048c8c', '#15161D', '#6b7775')) +
  scale_linetype_manual(values = c('solid', 'solid', 'dashed')) +
  theme_light() +
  labs(y = "Million barrels per day", 
       title = "OPEC Crude Oil Production has declined well below historical averages this year",
       subtitle = 'Month: {frame_along}',
       x = '',
       caption = '*Shaded area denotes 2015-2023 range') +
  scale_x_date(breaks = seq(min(chart_data$Month), max(chart_data$Month), by = '1 month'), date_labels = '%b') +
  transition_reveal(Month)


enter image description here


Solution

  • as_month <- function(date) format(date, "%b %Y")
    
    ...
    subtitle = 'Month: {as_month(frame_along)}',
    ...
    

    enter image description here