【问题标题】:forecast v7 & ggplot2 graphics adding fitted line to autoplot预测 v7 和 ggplot2 图形将拟合线添加到自动绘图
【发布时间】:2018-03-06 11:18:53
【问题描述】:

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

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

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

【问题讨论】:

  • autoplot(fc) + geom_line(aes(y = fitted(fc)), col = "red")
  • 当我尝试这个时,我得到这个错误:
  • 我的错,在你绘制之前添加library(ggfortify)

标签: r ggplot2 forecasting


【解决方案1】:

这里是另一个没有额外软件包的解决方案:

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 包,我的解决方案将起作用

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-11
    • 1970-01-01
    • 1970-01-01
    • 2012-10-23
    相关资源
    最近更新 更多