预测v7& ggplot2图形将拟合线添加到自动绘图 [英] forecast v7 & ggplot2 graphics adding fitted line to autoplot

查看:103
本文介绍了预测v7& ggplot2图形将拟合线添加到自动绘图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在浏览Rob J Hyndman的教程,这是该教程的链接,我的问题是如何向预测图添加拟合线,例如;

I was looking through a tutorial by Rob J Hyndman, here's the link for the tutorial, my question is how can i add a fitted line to the forecast plot eg;

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

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

I now need to add fitted(fc)to the plot above, how do I do it?

推荐答案

这里是没有其他软件包的其他解决方案:

Here an other solution without additional packages:

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软件包,我的解决方案就可以工作

You transform your ts into a data frame where you can use normal ggplot command. You can then just add the line. I realized that my solution work if the package ggfortify is not loaded

这篇关于预测v7&amp; ggplot2图形将拟合线添加到自动绘图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆