【问题标题】:Formatting quarterly dates in ggplot2在ggplot2中格式化季度日期
【发布时间】:2021-01-27 03:38:38
【问题描述】:

我有这张图:(厄瓜多尔的实际 GDP 周期)

这是构建它的代码:

df %>%
 filter(Serie %in% "GDP Cycle") %>%
 ggplot() +
 aes(x = quarters, y = Valor) +
 geom_line(size = 1L, colour = "#cb181d") +
 labs(x = "Trimestres", y = "% Standard Deviation from Trend", title = "Ciclo del PIB Real del Ecuador", subtitle = "Corte trimestral, Año Base 2007", caption = "Banco Central del Ecuador") +
 theme_light()+ theme(plot.title = element_text(face = "bold", size = 15))+ theme(plot.subtitle = element_text(size = 10))+
  geom_hline(yintercept = 0,linetype="dashed", size=1)

如何在 X 轴上显示每个季度的标签,如下所示:

2000.q1, 2000.q2, . . ., 2020.q1, 2020.q2, 2020.q3

这是数据示例:

df %>%
+     filter(Serie %in% "GDP Cycle")
# A tibble: 83 x 4
   counter quarters            Serie         Valor
     <dbl> <dttm>              <chr>         <dbl>
 1       1 2000-01-01 00:00:00 GDP Cycle -0.00973 
 2       2 2000-04-01 00:00:00 GDP Cycle -0.000653
 3       3 2000-07-01 00:00:00 GDP Cycle  0.0125  
 4       4 2000-10-01 00:00:00 GDP Cycle  0.0195  
 5       5 2001-01-01 00:00:00 GDP Cycle  0.00608 
 6       6 2001-04-01 00:00:00 GDP Cycle  0.00562 
 7       7 2001-07-01 00:00:00 GDP Cycle -0.00471 
 8       8 2001-10-01 00:00:00 GDP Cycle -0.00357 
 9       9 2002-01-01 00:00:00 GDP Cycle -0.00137 
10      10 2002-04-01 00:00:00 GDP Cycle  0.00144 
# … with 73 more rows

如何以这种格式获取 x 轴上的标签:“Jan 2000”到“Jul 2020”?

【问题讨论】:

标签: r datetime ggplot2


【解决方案1】:

由于quarters 是POSIXct 类型,您可以使用scale_x_datetime 并指定要显示x 轴值的格式和中断。

按月格式获取 x 轴上的标签:(“Jan 2000”到“Jul 2020”)

library(dplyr)
library(zoo)
library(zoo)

df %>%
  filter(Serie %in% "GDPCycle") %>%
  ggplot() +
  aes(x = quarters, y = Valor) +
  geom_line(size = 1L, colour = "#cb181d") +
  labs(x = "Trimestres", y = "% Standard Deviation from Trend", 
       title = "Ciclo del PIB Real del Ecuador", 
       subtitle = "Corte trimestral, Año Base 2007", 
       caption = "Banco Central del Ecuador") +
  theme_light()+ theme(plot.title = element_text(face = "bold", size = 15))+ 
  theme(plot.subtitle = element_text(size = 10))+
  geom_hline(yintercept = 0,linetype="dashed", size=1) +
  scale_x_datetime(date_labels = '%b %Y', date_breaks = '6 months')

要获取每个季度的 X 轴标签,例如 2000.q1、2000.q2,您可以将 POSIXct 时间转换为 yearqtr,然后使用scale_x_yearqtr

df %>%
  filter(Serie %in% "GDPCycle") %>%
  mutate(quarters = as.yearqtr(quarters)) %>%
  ggplot() +
  aes(x = quarters, y = Valor) +
  geom_line(size = 1L, colour = "#cb181d") +
  labs(x = "Trimestres", y = "% Standard Deviation from Trend", 
       title = "Ciclo del PIB Real del Ecuador", 
       subtitle = "Corte trimestral, Año Base 2007", 
       caption = "Banco Central del Ecuador") +
  theme_light()+ theme(plot.title = element_text(face = "bold", size = 15))+ 
  theme(plot.subtitle = element_text(size = 10))+
  geom_hline(yintercept = 0,linetype="dashed", size=1) +
  scale_x_yearqtr(format = '%Y.q%q')

【讨论】:

  • 天才!在第二张图中,我如何从 2000.q1 更改为 2000.t1 因为在西班牙季度是“三个月”(三个月)
  • 可以把最后一行改成scale_x_yearqtr(format = '%Y.t%q')
  • 发生了奇怪的事情,直接复制粘贴代码然后就没有红线了。
  • 一个小问题!如何扩展 x 轴上的“中断”?我想要更多的休息时间
  • 如果您想为scale_x_yearqtr 提供更多休息时间,您需要像这样指定n 值:scale_x_yearqtr(format = '%Y.q%q', n = 9)
【解决方案2】:

您可以使用scale_x_datetime 指定x 轴,并使用zoo::format.yearqtr 进行一些自定义以使格式正确。

library(zoo)
ggplot(df, aes(x = quarters, y = Valor)) +
  geom_line(size = 1L, colour = "#cb181d") +
  geom_hline(yintercept = 0,linetype="dashed", size=1) +
  scale_x_datetime(labels = function(x) zoo::format.yearqtr(x, "%Y.q%q")) + 
  labs(x = "Trimestres", y = "% Standard Deviation from Trend",
       title = "Ciclo del PIB Real del Ecuador",
       subtitle = "Corte trimestral, Año Base 2007",
       caption = "Banco Central del Ecuador") +
  theme_light() + 
  theme(plot.title = element_text(face = "bold", size = 15),
        plot.subtitle = element_text(size = 10))

【讨论】:

  • 如何扩展 x 轴的“中断”?
  • scale_x_datetime 接受 breaks = "3 months"breaks = "12 months" 的参数。默认为"6 months"
猜你喜欢
  • 1970-01-01
  • 2014-03-01
  • 2021-07-10
  • 2019-11-18
  • 2012-05-21
  • 2012-07-29
  • 1970-01-01
  • 2020-05-03
相关资源
最近更新 更多