0

I am working through section 2.2 "Time Plots" of publication Forecasting: Principles and Practice by Hyndman and Athanasopoulos (https://otexts.com/fpp3/time-plots.html) and I can't get the autoplot() function referenced therein to work. I assume autoplot() derives from the forecast package but as you can see in the below code I have also fiddled with ggplot2 and ggfortify packages and neither of these work either. The error I get when trying the forecast package is "Error in forecast::autoplot():! Objects of class <tbl_ts> are not supported by autoplot."

Any ideas how to get this autoplot() function working?

Here's the publication's autoplot() output that I'm trying to ape:

enter image description here

Code (in this example I was trying forecast package by using forecast::autoplot(...); further note that installing the tsibbledata package provides the "ansett" data used in this example):

library(dplyr)
library(tsibble)
library(tsibbledata)
library(forecast)
library(ggplot2)
library(ggfortify)

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

forecast::autoplot(melsyd_economy, Passengers) +
  labs(title = "Ansett airlines economy class",
       subtitle = "Melbourne-Sydney",
       y = "Passengers ('000)")

1 Answers1

1

The autoplot method for tsibbles is from the fabletools package. See Plot time series from a tsibble.

library(dplyr)
library(ggplot2)
library(tsibbledata)
library(fabletools)

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

autoplot(melsyd_economy, Passengers) +
  labs(
    title = "Ansett airlines economy class",
    subtitle = "Melbourne-Sydney",
    y = "Passengers ('000)"
  )

stefan
  • 90,330
  • 6
  • 25
  • 51
  • This also works for me using the feasts package – Curious Jorge - user9788072 Nov 30 '22 at 20:43
  • 1
    Yeah. Also when using `fable`. But the method is still from the `fabletools` package. But I have to admit that I have no clue why it works with feasts. Did not found anything in the package meta. – stefan Nov 30 '22 at 20:59
  • 1
    `autoplot()` is from the ggplot2 package, but is re-exported by forecast and fable. Both fable (``), and forecast (``) classes should work with `autoplot()` if you use `ggplot2::autoplot()`, or just `autoplot()`. If this doesn't work, you might need to update your packages or there is a bug that can be reported in the GitHub issues. – Mitchell O'Hara-Wild Nov 30 '22 at 22:05
  • 1
    @MitchellO'Hara-Wild The `autoplot` method from `ggplot2` does nothing as is evident from the source code (see https://github.com/tidyverse/ggplot2/blob/HEAD/R/autoplot.r). It simply "defines the S3 generic that other classes and packages can extend." That's what `fabletools` is doing. It defines an `autoplot` method for `tsibble`s, i.e. for objects of class `tbl_ts`. See https://github.com/tidyverts/fabletools/blob/master/R/plot.R. – stefan Nov 30 '22 at 22:26
  • Hi Mitchell, I completely reinstalled R and all packages (from CRAN) yesterday so I am now completely up-to-date with everything. – Curious Jorge - user9788072 Dec 01 '22 at 05:11
  • Yes, I am aware that ggplot2's `autoplot()` function just provides the generic, which is re-exported by the forecast and fabletools packages. I suggest using `ggplot2::autoplot()` incase there is an issue with the `forecast` or `fbl_ts` method registration. – Mitchell O'Hara-Wild Dec 01 '22 at 23:06