Forecast autoplot() function not working properly

I've been having some issues with the forecast package, the autoplot() function is not working properly for forecasted objects. Whenever I try setting parameters such as 'showgap = FALSE' it does not work and the gap keeps there. Yet, it's not plotting the prediction interval levels (80% and 95%). I tried loading only forecast package and running the code, however, nothing seems to work. I'm sorry if that's a silly question, but I could not figure it out on my on. All other packages are working properly, ggplot2 itself has no problem at all when plotting there (using other arguments, etc.).

Can anyone explain what's going on and how could it be fixed.

library(forecast)
#> Registered S3 method overwritten by 'quantmod':
#>   method            from
#>   as.zoo.data.frame zoo
library(fpp2)
#> ── Attaching packages ────────────────────────────────────────────── fpp2 2.5 ──
#> ✔ ggplot2   3.5.0     ✔ expsmooth 2.3  
#> ✔ fma       2.5
#> 
library(fpp3)
#> ── Attaching packages ────────────────────────────────────────────── fpp3 0.5 ──
#> ✔ tibble      3.2.1     ✔ tsibbledata 0.4.1
#> ✔ dplyr       1.1.3     ✔ feasts      0.3.1
#> ✔ tidyr       1.3.0     ✔ fable       0.3.3
#> ✔ lubridate   1.9.2     ✔ fabletools  0.3.3
#> ✔ tsibble     1.1.3
#> ── Conflicts ───────────────────────────────────────────────── fpp3_conflicts ──
#> ✖ lubridate::date()    masks base::date()
#> ✖ dplyr::filter()      masks stats::filter()
#> ✖ tsibble::intersect() masks base::intersect()
#> ✖ tsibble::interval()  masks lubridate::interval()
#> ✖ dplyr::lag()         masks stats::lag()
#> ✖ tsibble::setdiff()   masks base::setdiff()
#> ✖ tsibble::union()     masks base::union()
#> 
#> Attaching package: 'fpp3'
#> The following object is masked from 'package:fpp2':
#> 
#>     insurance

a <- ts(c(115,93,85,47,72,88,91,111,175,189), start = 2015)

forecast.a <- forecast(a, h = 7, level = c(80,95))
autoplot(forecast.a, showgap = FALSE)

Created on 2024-03-31 with reprex v2.1.0

Session info
sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value
#>  version  R version 4.3.3 (2024-02-29 ucrt)
#>  os       Windows 11 x64 (build 22631)
#>  system   x86_64, mingw32
#>  ui       RTerm
#>  language (EN)
#>  collate  Portuguese_Brazil.utf8
#>  ctype    Portuguese_Brazil.utf8
#>  tz       America/Fortaleza
#>  date     2024-03-31
#>  pandoc   3.1.1 @ C:/Program Files/RStudio/resources/app/bin/quarto/bin/tools/ (via rmarkdown)

You are creating forecast.a which is an object of class forecast. So when you call autoplot(), it uses the forecast:::autoplot.forecast() method. That particular function has no showgaps argument, and it does not produce a legend.

There is no need to load the fpp2 and fpp3 packages in this code, and you should not be using them together in any case. fpp2 provides functions and code for use with the 2nd edition of the textbook Forecasting: Principles & Practice (Forecasting: Principles and Practice (2nd ed)), while fpp3 provides functions and code for use with the 3rd edition of the textbook Forecasting: Principles & Practice (http://OTexts.com/fpp3.).

You will notice that there are no legends in the plots in the 2nd edition of the book, but there are legends in the 3rd edition of the book. That's because the 3rd edition is using fable objects rather than forecast objects, and therefore a different autoplot method is being called.

2 Likes

Thanks, professor. I did not pay attention to the details you pointed out. Thanks for replying, it was exactly what I was looking for.

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.