R 将预测集成到ggplot2中
我试图用给定数据的预测来完成基于时间的数据帧。我的数据框如下所示:R 将预测集成到ggplot2中,r,ggplot2,R,Ggplot2,我试图用给定数据的预测来完成基于时间的数据帧。我的数据框如下所示: df <- data.frame(duration = c(2000,2001,2002,2003,2004,2005), values = c(120,130,140,100,150,170), values2 = c(values = c(10,30,40,10,15,17))) df不太确定这是否是forecast背后的功能。 我会这样做的 l
df <- data.frame(duration = c(2000,2001,2002,2003,2004,2005),
values = c(120,130,140,100,150,170),
values2 = c(values = c(10,30,40,10,15,17)))
df不太确定这是否是forecast
背后的功能。
我会这样做的
library(reshape2)
library(forecast)
library(ggplot2)
library(dplyr)
vc <- data.frame(forecast(df$values))
tc <- data.frame(forecast(df$values2))
# Bind them
df2 <- rbind(vc,tc)
# Name them
df2$variable <- c(rep('values', 10), rep('values2', 10))
# Assign duration for plotting
df2$duration <- rep(2006:2015, 2) # not sure if the years are correct
# Melt original data
m_df <- melt(df, id = 'duration')
结果
我认为您有输入错误,tc
和vc
都使用值
而不是值
和值2
,对吗?
library(reshape2)
library(forecast)
library(ggplot2)
library(dplyr)
vc <- data.frame(forecast(df$values))
tc <- data.frame(forecast(df$values2))
# Bind them
df2 <- rbind(vc,tc)
# Name them
df2$variable <- c(rep('values', 10), rep('values2', 10))
# Assign duration for plotting
df2$duration <- rep(2006:2015, 2) # not sure if the years are correct
# Melt original data
m_df <- melt(df, id = 'duration')
# Plot
ggplot(full_join(m_df, df2),
aes(duration, value)) +
facet_wrap(~variable) +
geom_point() +
geom_line() + geom_line(aes(duration, Point.Forecast), col='red')