rggplot2tsibble

Changing labels and breaks with ggplot2 and tsibble


Since we can't use scale_x_date() with a {tsibble} object, what's the approach to change breaks, labels etc?

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

Solution

  • The issue is not the tsibble, the issue is that your Week variable isn't of class Date. But you could convert it to a Date to make scale_x_date work:

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