【问题标题】:Changing labels and breaks with ggplot2 and tsibble使用 ggplot2 和 tsibble 更改标签和中断
【发布时间】:2021-10-05 14:09:29
【问题描述】:

既然我们不能将scale_x_date(){tsibble} 对象一起使用,那么更改中断、标签等的方法是什么?

# metapackage
library(fpp3)

# data
melsyd_economy <- ansett %>%
  filter(Airports == "MEL-SYD", Class == "Economy") %>%
  mutate(Passengers = Passengers/1000)

# plot
autoplot(melsyd_economy, Passengers) +
  labs(title = "Ansett airlines economy class",
       subtitle = "Melbourne-Sydney",
       y = "Passengers ('000)") +
  scale_x_date(date_labels = "%Y", date_breaks = "2 year")

【问题讨论】:

    标签: r ggplot2 tsibble


    【解决方案1】:

    问题不在于tsibble,问题在于您的Week 变量不属于Date 类。但您可以将其转换为 Date 以使 scale_x_date 工作:

    # metapackage
    library(fpp3)
    
    # data
    melsyd_economy <- ansett %>%
      filter(Airports == "MEL-SYD", Class == "Economy") %>%
      mutate(Passengers = Passengers/1000) %>% 
      mutate(Week = lubridate::as_date(Week))
    
    # plot
    autoplot(melsyd_economy, Passengers) +
      labs(title = "Ansett airlines economy class",
           subtitle = "Melbourne-Sydney",
           y = "Passengers ('000)") +
      scale_x_date(date_labels = "%Y", date_breaks = "2 year")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-02-03
      • 2021-01-02
      • 1970-01-01
      • 2016-02-05
      • 1970-01-01
      • 1970-01-01
      • 2011-04-19
      • 2021-07-23
      相关资源
      最近更新 更多