预测 v7 和 ggplot2 图形将拟合线添加到自动绘图

forecast v7 & ggplot2 graphics adding fitted line to autoplot

我正在浏览 Rob J Hyndman 的教程,这里是 the link for the tutorial,我的问题是如何向预测图中添加拟合线,例如;

library(forecast)
library(ggplot2)
fc <- forecast(fdeaths)
autoplot(fc)

我现在需要将 fitted(fc) 添加到上面的图中,我该怎么做?

这里是另一个没有附加包的解决方案:

fit <- data.frame(data=as.matrix(fitted(fc)), date=time(fitted(fc)))
autoplot(fc) + geom_line(data = fit,aes(date,data), col = "red")

您将 ts 转换为数据框,您可以在其中使用普通的 ggplot 命令。然后您可以只添加该行。 我意识到如果未加载包 ggfortify,我的解决方案就可以工作