Can not use autoplot with a tsibble

I am following Rob Hyndman forecasting book and I can not use autoplot even in this simple example, extracted from the book:

library(ggplot2)
library(tsibble)
y <- tsibble(Year = 2015:2019,
             Observation = c(123,39,78,52,110),
             index = Year)
autoplot(y)

I got the following error: Error: Objects of type tbl_ts/tbl_df/tbl/data.frame not supported by autoplot . I have for sure that y is a tsibble :

y
# A tsibble: 5 x 2 [1Y]
   Year Observation
  <int>       <dbl>
1  2015         123
2  2016          39
3  2017          78
4  2018          52
5  2019         110
1 Like

Hi @sbac. You have to load the ggfortify library to extend the autoplot function. And ggfortify support the ts class but not tbl_ts. So, you have to change the class to ts by as.ts function.

library(ggplot2)
library(tsibble)
library(ggfortify)
y <- tsibble(Year = 2015:2019,
             Observation = c(123,39,78,52,110),
             index = Year)
autoplot(as.ts(y))

Created on 2019-10-02 by the reprex package (v0.3.0)

1 Like

The tsibble package is part of a family of packages, and the functions for graphics are in the feasts package. So just load the feasts package as well and you can autoplot a tsibble object.

library(ggplot2)
library(tsibble)
library(feasts)
#> Loading required package: fabletools
#> 
#> Attaching package: 'fabletools'
#> The following object is masked from 'package:stats':
#> 
#>     decompose
y <- tsibble(Year = 2015:2019,
             Observation = c(123,39,78,52,110),
             index = Year)
autoplot(y, Observation)

Created on 2019-10-03 by the reprex package (v0.3.0)

7 Likes

I would not recommend ggfortify. It tends to overwrite functions in other packages, often with inferior functions. The autoplot functions in the feasts package (for tsibble objects) and in the forecast package (for ts objects) are better.

7 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.