【发布时间】:2014-06-02 20:08:34
【问题描述】:
我有一个数据集 sales_history。这是前 15 行中的 dput
sales_history <- structure(list(month = c("2008/01", "2008/02",
"2008/03", "2008/04", "2008/05", "2008/06", "2008/07",
"2008/08", "2008/09", "2008/10", "2008/11", "2008/12",
"2009/01", "2009/02", "2009/03"),
sales= c(941, 1275, 1908, 2152, 1556,
3052, 2627, 3244, 3817, 3580, 444,
3332, 2823, 3407, 4148 )),
.Names = c("month", "sales"),
row.names = c(NA, 15L),
class = "data.frame")
我有几个月从 2008/01 到 2013/10。我使用以下方法对其进行了自动 arima 预测:
arimaforecast<-function(df)
{
ts1<- ts(df$sales, frequency=12, start=c(2008,1))
fit<-auto.arima(ts1,ic="bic")
plot1=plot(forecast(fit,h=20))
return(plot1)
}
arimaforecast(sales_history)
然后我想绘制时间序列。我写如下。
y <- ts(sales_history$sales,freq=12,start=c(2011,1),end=c(2013,10))
yt <- window(y,end=c(2013,4))
yfit <- auto.arima(yt,ic="bic")
yfor <- forecast(yfit,h=10)
plot(yfor, main="sales Forecasting", sub="OPTIMAL ARIMA MODEL",
xlab="MONTH", ylab="sales")
lines(fitted(yfor),col="blue")
lines(y,col="red")
然后,图表变得非常难看。如何生成更好的图表来执行以下操作?
- y 轴不显示为 1e+06、3e+06,而是显示为 1M、3M 等。另外,
- 使用绿色直方图(即条形图)显示历史销售数据,同时仍使用线(带有连接点)显示拟合历史和预测?
【问题讨论】:
-
您能提供一个示例数据集吗? sales_history 数据框来自哪里?或者,您可以使用
dput(sales_history)提供您的数据(如果数据很多,则使用其中的一个子集)。 -
@sebkopf 使用 dput(head(sales_history,n=15)) 给出以下输出: structure(list(month = c("2008/01", "2008/02", "2008/ 03”、“2008/04”、“2008/05”、“2008/06”、“2008/07”、“2008/08”、“2008/09”、“2008/10”、“2008/11” , "2008/12", "2009/01", "2009/02", "2009/03"), 销售额 = c(941628, 1277005, 1908769, 2152362, 1556356, 3052123, 2627250, 3244551, 3817610, 3817610, 3817610, 4447715, 3332705, 2823324, 3407557, 4148698 )), .Names = c("month", "sales"), row.names = c(NA, 15L), class= "data.frame") 谢谢!
-
真的吗?把它放在问题中。它在 cmets 中有什么用处?
-
@DavidArenburg 感谢提醒;刚刚更新了问题。
标签: r plot time-series