I am doing time series analysis in R using the forecast package. The forecast::autoplot() creates a ggplot object.
I have two questions about plotting decomposed series:
Here is my code:
library(forecast)
# Load in AirPassengers time series
data("AirPassengers")
# Decomopose time series
decomposed_ts <- decompose(AirPassengers)
# Plot decomposed time series
forecast::autoplot(decomposed_ts) + geom_vline(xintercept = 1954, colour = "red")
For Q1, I tried to use annotate() but it places the same text labels on each facet, whereas I only want one label on only the trend panel (see screenshot). I also tried to use the ggtext package, but could not get it to work. I couldn't figure out what the faceted variable is because forecast::autoplot() automatically created the faceting.
For Q2. I saved the plot as a variable i.e. myPlot <- forecast::autoplot(decomposed_ts) + geom_vline(xintercept = 1954, colour = "red"). I then tried myPlot$ looking for where the "data" facet is named.
You can add geom_text to the original auto_plot:
annotation <- data.frame(
x = c(1950),
y = c(250),
parts = c("trend"),
label = c("label 1")
)
forecast::autoplot(decomposed_ts) +
geom_vline(xintercept = 1954, colour = "red") +
geom_text(data = annotation, aes(x = x, y = y, label = label))