【问题标题】:Wrong years in quarterly data scales季度数据规模错误的年份
【发布时间】:2021-06-25 01:46:17
【问题描述】:

我的数据集如下所示:

> head(df,10)
# A tibble: 10 x 2
   quarters            `GDP Cycle`
   <dttm>                    <dbl>
 1 2000-01-01 00:00:00   -0.00973 
 2 2000-04-01 00:00:00   -0.000653
 3 2000-07-01 00:00:00    0.0125  
 4 2000-10-01 00:00:00    0.0195  
 5 2001-01-01 00:00:00    0.00608 
 6 2001-04-01 00:00:00    0.00562 
 7 2001-07-01 00:00:00   -0.00471 
 8 2001-10-01 00:00:00   -0.00357 
 9 2002-01-01 00:00:00   -0.00137 
10 2002-04-01 00:00:00    0.00144 

我正在 ggplot2 上使用此代码来制作此图表:

df %>%
 filter(quarters >= "2014-04-01 09:04:23" & quarters <= "2020-07-01 00:00:00") %>%
 ggplot() +
 aes(x = quarters, y = `GDP Cycle`) +
 geom_line(size = 1L, colour = "#cb181d") +
 labs(x = "Quarters", y = "% Standard deviation from trend", title = "Ecuador's Real GDP Cycle", subtitle = "Hodrick Prescott Filter | λ=1600 ", caption = "BCE GDP") +
 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',n=5)

问题是x轴,我需要像“2000.q1”这样的。

【问题讨论】:

    标签: r date datetime ggplot2


    【解决方案1】:

    这在手册中并不是特别明显,但是如果你把你的日期改成yearqtr它就可以了。我会说这是一个(未记录的)弱点,您可能想给他们写一行并仔细检查这是否是它的预期工作方式。

    实现这一点的一种方法是在数据上添加更多链接的东西:

    (记得在过滤日期后唱出类型!)

    
    df %>% filter(quarters >= "2014-04-01 09:04:23" & quarters <= "2020-07-01 00:00:00") %>% mutate( quarters = as.yearqtr(quarters) ) %>%
        ggplot( aes(x = quarters, y = `GDP Cycle`) ) +
        geom_line(size = 1L, colour = "#cb181d") +
        labs(x = "Quarters", y = "% Standard deviation from trend", title = "Ecuador's Real GDP Cycle", subtitle = "Hodrick Prescott Filter | λ=1600 ", caption = "BCE GDP") +
        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 = '%Yq%q',n=5) +
        geom_blank()
    
    

    【讨论】:

    • 很抱歉迟到了一个月,这很好用。谢谢!!
    猜你喜欢
    • 1970-01-01
    • 2020-07-16
    • 2018-12-20
    • 1970-01-01
    • 1970-01-01
    • 2020-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多