Year-Month display in autoplot

I want to display the year and month (e.g.: 2008-06, 2008-09, 2008-12, ‥) on the x-axis label when plotting the prediction result of time series analysis with autoplot.
(By default, the output will be as follows)

library(forecast)

#> Data
my_ts <- ts(data = c(410, 488, 415, 398, 419, 488, 414, 374),
            start = c(2008, 3), frequency = 4)

#> Forecast
fc <- hw(my_ts, seasonal = "additive", h = 2)

#> Plot
autoplot(fc)

What should I do?

One option is using ggfortify to convert forecast object to data frame then let ggplot2 do the plotting. I found a few examples here and here

One option is to use the tsibble and fable packages, rather than the forecast package. Here is an example using your data.

library(tsibble)
library(fable)
library(ggplot2)

# Data
my_ts <- ts(data = c(410, 488, 415, 398, 419, 488, 414, 374),
            start = c(2008, 3), frequency = 4) %>%
  as_tsibble()
my_ts
#> # A tsibble: 8 x 2 [1Q]
#>     index value
#>     <qtr> <dbl>
#> 1 2008 Q3   410
#> 2 2008 Q4   488
#> 3 2009 Q1   415
#> 4 2009 Q2   398
#> 5 2009 Q3   419
#> 6 2009 Q4   488
#> 7 2010 Q1   414
#> 8 2010 Q2   374

# Forecast
fc <- my_ts %>%
  model(hw = ETS(value ~ season("A"))) %>%
  forecast(h=2)

# Plot
fc %>% autoplot(my_ts) +
  labs(x = "Quarter")

Created on 2021-03-25 by the reprex package (v1.0.0)

Thank you for sharing the references.
I tried as follows.

library(tidyverse)
library(ggfortify)
library(lubridate)

# to data.frame
for_plot <- fortify(fc)

# passing 5 months
for_plot <- for_plot %>% 
  mutate(Index = ymd(Index) %m+% period("5 month"))

# Plot
ggplot(data = for_plot) +
  geom_line(aes(x= Index, y = Data, color = "raw")) +
  geom_line(aes(x= Index, y = `Point Forecast`, color = "point forecast")) +
  geom_ribbon(aes(x= Index, ymin = `Lo 80`, ymax = `Hi 80`,  fill = "80"),  alpha = 0.2) +
  geom_ribbon(aes(x= Index, ymin = `Lo 95`, ymax = `Hi 95`,  fill = "95"),  alpha = 0.2) +
  scale_fill_manual("what", values = c("blue", "dodgerblue"))+
  scale_color_manual("why", values = c("red", "black"))


I want to display the x-axis labels as "2008-12", "2009-06", "2009-12", "..", but displayed as "2009-01", "2009-07", "2010-01", "..".
How should I fix it?

Dear Professor Hyndman,

Thank you for your answering.
(It seems very nice!)
But I have a question.
Why is the prediction intervals different(getting wider) from the first ones?

Actually, there wasn't really enough data to estimate the full HW model, even though the forecast package tried to do so resulting in an over-fit with variances underestimated. The fable package is a little stricter and it fitted an equivalent model but without a trend term. This model can be estimated properly so the prediction intervals are more realistic.

I see. (Thank you!)
In the original method, I can check the values of various parameters(e.g.:alpha, beta, gamma, l, b, s) by executing "summary(fc)".
In this method, how can I do that?

my_ts %>%
  model(hw = ETS(value ~ season("A"))) %>%
  tidy()

Thank you!
Now, I have two additional questions.
Q1. For quarterly data, only 3 parameters of s(s0 to s2) are displayed and the total is not 0. How should I understand?
Q2. How can I get the lower(or upper) limit of the 80(or 95)% prediction intervals?

#>  # A tibble: 6 x 3
#>  .model term    estimate
#>  <chr>  <chr>      <dbl>
#> 1 hw     alpha   0.000100
#> 2 hw     gamma   0.878   
#> 3 hw     l     427.      
#> 4 hw     s0    -32.5     
#> 5 hw     s1    -11.8     
#> 6 hw     s2     60.3

Because the sum of the seasonal components is 0, the last one is redundant. It is equal to minus the sum of the others.

See 5.5 Distributional forecasts and prediction intervals | Forecasting: Principles and Practice (3rd ed) for getting prediction intervals.

Thank you!!
I didn't notice that the 3rd edition was released.
I will refer to it.

this is really good, thank you for sharing with us get-vidmateapp.com get-mobdroapk.com

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.