【问题标题】:X axis in DateTime format in R script/plotR脚本/绘图中日期时间格式的X轴
【发布时间】:2016-12-14 00:54:56
【问题描述】:

我正在尝试在 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 轴是句点(数字)而不是数据格式。

非常感谢任何帮助。

【问题讨论】:

    标签: r plot time-series powerbi rscript


    【解决方案1】:
    • 尝试在绘图中输入明确的规范:plot(x=Date, ...)
    • 如果它不起作用,请尝试:

      时间线 plot(x=...,y=..., xlab=NA, xaxt="n") # 没有 x 轴 axis.Date(1, at=(timeline), format=F, labels=TRUE) # 特殊轴

    编辑: 对不起,我的第一个解决方案不适合您的时代。问题是没有日期是时间序列,而是引用“开始”和“频率”的索引。在这里,您的问题来自您对“频率”的使用,它应该按时间单位指定观察次数,即季度数据为 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() 以指定这些数据是每日数据,并调整您的预测函数。

    【讨论】:

    • 嗨,在我的情况下,我如何确定 'x = '。我想说 plot (x= Date, y = (Pred) , xlab= NA, xaxt = "n") 。但这会引发错误,指出日期无法识别。
    猜你喜欢
    • 2020-12-07
    • 1970-01-01
    • 2011-05-17
    • 2016-07-29
    • 1970-01-01
    • 1970-01-01
    • 2017-04-09
    • 1970-01-01
    • 2013-04-06
    相关资源
    最近更新 更多