首页 > 解决方案 > 从预测包中消除自动绘图中历史和预测之间的差距

问题描述

?autoplot.forecast
If showgap=FALSE, the gap between the historical observations and the forecasts is removed.

但是,对我来说,它并没有被删除。

library(forecast)

d <- ts(rnorm(36), start = c(2021, 1), frequency = 12)

fc <- snaive(d, h = 12)

autoplot(fc, showgap = FALSE)

但是使用plot效果很好

plot(fc, showgap = FALSE)

标签: rggplot2forecastautoplot

解决方案


OP,我原本以为这是forecast::autoplot()函数中的一个错误,但事实证明不是。在对象上plot.forecast()使用时运行的函数包含参数。该命令确实指示了参数,但不是为了- 它是针对函数的。plot()'forecast'showgap=?autoplot.forecastshowgap=autoplot()plot.forecast()

好消息是其中似乎有一个autolayer(...)包含此参数的方法,您可以使用它。即再次检查帮助plot.forecast()有点低,你会看到这个:

## S3 method for class 'forecast'
autolayer(object, series = NULL, PI = TRUE, showgap = TRUE, ...)

我们可以autolayer()autoplot(). 所以,这似乎有效:

autoplot(fc) + autolayer(fc, showgap = F)

在此处输入图像描述


推荐阅读