0

I have created a time series model using package forecast and have plotted the observations and forecast values using autoplot.

require(ggplot2)
require(forecast)
y <- hw(AirPassengers, h=10, seasonal="multiplicative")
autoplot(y, main="Air passengers - Holt seasonal") +
  autolayer(y, series="multiplicative", PI=FALSE)

Time series with future forecast values

However, I am looking to label the observations like so: Variable label position in ggplot line chart but only for the forecast values. I understand the clause ought to be geom_text( aes(label = ?) ) but since the object y being passed onto autoplot is a ts I am unsure as to how to reference ?

I have resorted to as.numeric(y[["mean"]]) but got the error Error: geom_text requires the following missing aesthetics: x, y since this is a vector and dates are missing?

Thank you.

Sweepy Dodo
  • 1,761
  • 9
  • 15
  • You find the values here: `ggplot_build(p)$data[[4]]$y` where `p` is your plot. Pass `ggplot_build(p)$data` to the `data` argument in `geom_text()` – markus Jul 18 '19 at 08:54
  • 1
    p + geom_text( aes(x,y, label=y), data=ggplot_build(p)$data[[3]] ) The above worked. Thank you for pointing me to right direction @markus – Sweepy Dodo Jul 18 '19 at 14:40

0 Answers0