You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在R的gganimate包中格式化frame_along的日期为仅显示月份

解决gganimate副标题日期格式化问题

问题背景

你在R语言中使用gganimate包制作动画图表时,副标题里的{frame_along}会显示完整日期,希望将其格式化为仅显示月份名称(缩写或完整形式)。

你的原始数据框和代码如下:

数据框

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"))

原始动画代码

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)

解决方案

直接在副标题的表达式中使用format()函数对frame_along进行日期格式化,指定想要的月份格式即可:

修改后的完整代码

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",
       # 格式化frame_along为月份缩写
       subtitle = 'Month: {format(frame_along, "%b")}',
       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)

格式说明

  • %b:输出月份的英文缩写(例如Jan、Feb、Mar)
  • 如果需要显示完整月份名称,改用"%B"(例如January、February)

内容的提问来源于stack exchange,提问作者Tanga94

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 00:09:49