R脚本/图中的日期时间格式的X轴

时间:2016-08-08 09:46:34

标签: r plot time-series powerbi rscript

我正在尝试在R中建立一个预测图。但是,尽管尝试了许多解决方案,但我无法在日期中绘制我的X轴。

我的数据形式为:

Datetime(MM/DD/YYY) ConsumedSpace
01-01-2015          2488
02-01-2015          7484
03-01-2015          4747

以下是我正在使用的预测脚本:

library(forecast)
library(calibrate)
# group searches by date
dataset <- aggregate(ConsumedSpace ~ Date, data = dataset, FUN= sum)
# create a time series based on day of week
ts <- ts(dataset$ConsumedSpace, frequency=6)
# pull out the seasonal, trend, and irregular components from the time series (train the forecast model)
decom <- stl(ts, s.window = "periodic")
#predict the next 7 days of searches
Pred <- forecast(decom)
# plot the forecast model
plot(Pred)
#text(Pred,ts ,labels = dataset$ConsumedSpace)

输出看起来像这样 - 你可以看到我显示的X轴是周期(数字)而不是数据格式。 enter image description here

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

  • 尝试在地图中输入明确的规范:plot(x=Date, ...)
  • 如果不起作用,请尝试:

    时间轴&lt; -seq(从= your.first.date,到= your.last.date,by =“week”)
    plot(x = ...,y = ...,xlab = NA,xaxt =“n”)#no x轴 axis.Date(1,at =(timeline),format = F,labels = TRUE)#Special axis

编辑: 对不起,我的第一个解决方案,不适合你的时间表。问题是没有日期是时间序列,而是指向“开始”和“频率”的索引。在这里,您的问题来自您使用“频率”,它应该按时间单位指定观察数量,即季度数据为4,月度数据为12 ......这里您的时间单位为周, 6个开放日,这就是为什么你的图形轴表示指数好几周。要想有一个更易读的轴,你可以试试这个:

dmin<-as.Date("2015-01-01")    # Starting date
# Dummy data
ConsumedSpace=rep(c(5488, 7484, 4747, 4900, 4747, 6548, 6548, 7400, 6300, 8484, 5161, 6161),2)

ts<-ts(ConsumedSpace, frequency=6)
decom <- stl(ts, s.window = "periodic")
Pred <- forecast(decom)

plot(Pred, xlab=NA, xaxt="n")   # Plot with no axis
ticks<-seq(from=dmin, to= dmin+(length(time(Pred))-1)*7, by = 7) # Ticks sequency : ie weeks label
axis(1, at=time(Pred), labels=ticks) # axis with weeks label at weeks index

由于关闭日,您必须为周标签使用7个间隔。 这很难看,但它确实有效。肯定有更好的方法仔细查看您的ts(),以指定这些数据是每日数据,并调整您的预测功能。