0

The following simplified code is creating a time series and building an autoplot with the Holt's model:

library(dplyr) 
library(tidyverse)
library(fpp2)

df <-data.frame(y = c(85.1,86.0,86.5,86.9,87.2), 
x = 1:5)

mdl <-ts(df$y) %>%
holt()
ggplot2::autoplot(mdl)

We get this graph:

enter image description here

Earlier I was using plotFit from investr package to plot fitted models. However plotFit can't plot holt() models. That is why I am currently shifting to ggplot(). The first step is to build a ggplot with curve, prediction and confidence bands with specified colours and other thin adjustments. I began with plotting the model with autoplot(). But I got stuck trying to extract specific geoms' scripts used by autoplot to build the layers of underlying ggplot - curve, prediction and confidence bands. By the way if I am not mistaken upon investigation using layer_data() only two bands (.8 and .95 levels) with a single band type are plotted by autoplot().

I wonder if it is possible to extract the required information from autoplot() object? Or what geoms should I use for the alike ggplot built from scratch.

Hereunder I attach the desired simplified output. The sample is based on lm() model plotted with plotFit():

library(investr) 
mdl2 <-lm(df, formula = y~x) 
plotFit(mdl2, interval = "both", 
col.conf = adjustcolor("orange", 0.6),
col.pred = adjustcolor("orange", 0.3), 
col = adjustcolor("orange", 0.9), 
shade = T) 

This code is giving the following output:

enter image description here

asd-tm
  • 3,381
  • 2
  • 24
  • 41

0 Answers0