【问题标题】:Edit time series plot编辑时间序列图
【发布时间】: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")

然后,图表变得非常难看。如何生成更好的图表来执行以下操作?

  1. y 轴不显示为 1e+06、3e+06,而是显示为 1M、3M 等。另外,
  2. 使用绿色直方图(即条形图)显示历史销售数据,同时仍使用线(带有连接点)显示拟合历史和预测?

【问题讨论】:

  • 您能提供一个示例数据集吗? 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


【解决方案1】:

我仍然不完全确定您对条形图的含义,因为您的图表中没有绿线(只有蓝色和红色),但这是一个结合了我认为您正在寻找的不同特征的情节为了。由于绘图有点复杂,而且我对forecast 提供的绘图功能不太熟悉,因此这是使用ggplot2 包实现的,它可以制作非常漂亮的图表并提供很大的调整灵活性(请参阅ggplo2 获取详细文档)。

代码的第一部分从您的代码示例中获取预测对象yfor,并将其转换为易于在ggplot 中使用的数据框(您可以通过使用日期对象而不是数字时间刻度来改进此部分如果您想在 x 轴标签上获得更大的灵活性),第二部分将绘制它(绘图被截断,因为这仅适用于您的数据的一个子集,但也适用于整个数据集)。

# convert forecast object into data frame
ts_values <- data.frame(
    time = as.numeric(time(yfor$x)),  
    sales = as.numeric(yfor$x), 
    fit = as.numeric(yfor$fitted))
ts_forecast <- data.frame(
    time = as.numeric(time(yfor$mean)),
    fit = as.numeric(yfor$mean),
    upper.80 = as.numeric(yfor$upper[,1]),
    upper.95 = as.numeric(yfor$upper[,2]),
    lower.80 = as.numeric(yfor$lower[,1]),
    lower.95 = as.numeric(yfor$lower[,2]))

# combine fitted data and forecast mean
ts_values <- rbind(ts_values, transform(ts_forecast[c("time", "fit")], sales = NA))

# plot it all
library(ggplot2)
ggplot(NULL, aes(x = time)) + 
    geom_bar(data = ts_values, aes(y = sales), stat = "identity", 
             fill = "dark green", position="dodge") + 
    geom_line(data = ts_values, aes(y = fit), colour = "red", size = 2) + 
    geom_ribbon(data = ts_forecast, aes(ymin = lower.95,  ymax = upper.95),  
                alpha=.2,  fill="red") + 
    geom_ribbon(data = ts_forecast, aes(ymin = lower.80,  ymax = upper.80),  
                alpha=.2,  fill="red") +
    scale_y_continuous(labels = function(x) paste(x/10^6, "M"), expand = c(0,0)) +
    theme_bw()

【讨论】:

  • 这太棒了,正是我想要的。谢谢!
猜你喜欢
  • 2021-10-25
  • 1970-01-01
  • 2023-03-04
  • 2010-12-23
  • 1970-01-01
  • 2022-11-15
  • 2021-10-31
  • 2017-10-16
  • 2015-06-15
相关资源
最近更新 更多