【问题标题】:Add months to plot of forecast在预测图中添加月份
【发布时间】:2019-08-30 23:40:12
【问题描述】:

我使用 2017 年 1 月至 2019 年 7 月的月度数据以及用于指数平滑的创新状态空间模型生成了一年的预测。

library("forecast") 
library("ggplot2")
library("scales")

myData<-c(11878838,11025085,13346905,11657777,12670973,12321707,10668589,13296366,10976129,13383487,11157827,9064816,
          10712529,11058177,11220417,12010494,11824792,10623115,10171066,13388616,11249052,14134987,10795036,9001540,
          10561734,10658972,11689656,10351909,11062358,9858083,9779142)

ts_myData<-ts(myData, frequency=12, start=c(2017,1), end=c(2019,7))

model<-ets(ts_myData, model="MNM")

fcast_units<-forecast(model, h=12, PI=TRUE)  

autoplot(fcast_units)+
  xlab("Year")+
  ylab("Number of units")+
  ggtitle("My forecast")+
  scale_y_continuous(label=comma) # this line is to avoid scientific notation and use the format you see on the y-label

问题是我想更改绘图的 x 轴,但似乎无法弄清楚如何。我想查看从 2017 年 1 月到 2020 年 7 月的所有月份。现在情节看起来像这样。

如果你看一下当前的情节,你可以推断出白色的垂直线代表了一年的开始和结束以及六月,但我希望每年都有 12 个。我尝试使用scale_x_datescale_x_datetime 但它们不起作用(也许我没有正确指定参数?)。也许问题是fcast 属于forecast?我不确定。

感谢任何帮助。

【问题讨论】:

  • 检查 [geom_vline](www.sthda.com/english/wiki/ggplot2-add-straight-lines-to-a-plot-horizo​​ntal-vertical-and-regression-lines)为你的情况做点什么。

标签: r ggplot2 forecasting


【解决方案1】:

这种代码很有效。

autoplot(fcast_units)+
  xlab("Year")+
  ylab("Number of units")+
  ggtitle("My forecast")+
  scale_y_continuous(label=comma)+
  scale_x_continuous(breaks = seq(2017 , 2020, 1/6))

【讨论】:

    猜你喜欢
    • 2020-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-30
    • 2018-03-26
    • 2020-05-12
    相关资源
    最近更新 更多