简体   繁体   English

以 x 轴标签为日期的预测 plot

[英]Forecast plot with x axis labels as date

I have a dataset like revenue and date.我有一个像收入和日期这样的数据集。

I used arima to plot the data.我用 arima 来 plot 的数据。

ts_data = ts(dataset$Revenue,frequency = 7)
arima.ts = auto.arima(ts_data)
pred = forecast(arima.ts,h=30)
plot(pred,xaxt="n")

When I plot the data, it produces plot like below.当我 plot 数据时,它会产生 plot 如下所示。 在此处输入图像描述

My expectations are below,我的期望如下,

I need to display values in Million for predicted values like 13.1M.对于 13.1M 等预测值,我需要以百万为单位显示值。 I need to show x-axis as date instead of data points numbers.我需要将 x 轴显示为日期而不是数据点数字。

I tried several links but couldn't crack it.我尝试了几个链接,但无法破解它。 Below are the experiments I made,下面是我做的实验,

Tried with start date and end date in ts_data that also doesnt work.My start date is "2019-09-27" and end date is "2020-07-02"尝试使用 ts_data 中的开始日期和结束日期也不起作用。我的开始日期是“2019-09-27”,结束日期是“2020-07-02”

tried wit axis_date in plot function that also doesnt work.尝试在 plot function 中使用 axis_date 也不起作用。

Please help me to crack the same.请帮我破解同样的问题。

Thanks a lot.非常感谢。

You can specify axis tick values and labels with axis()您可以使用 axis() 指定轴刻度值和标签

plot(pred,xaxt="n", yaxt="n")  # deactivate x and y axis
yl<-seq(-5000000,15000000,by=5000000)   # position of y-axis ticks
axis(2, at=yl, label=paste(yl/1000000, "M"))  # 2 = left axis

You can specify the desired position of y axis ticks with at and the labels to be associated with label .您可以使用at指定所需的 y 轴刻度 position 以及与label关联的标签。 In order to obtain the values like 10 MI have used the function paste to join the numbers with the letter M.为了获得像 10 MI 这样的值,使用了 function 粘贴将带有字母 M 的数字连接起来。

You could use the same method also for x-axis, even tough more efficient methods probably exist.您也可以对 x 轴使用相同的方法,甚至可能存在更有效的方法。 Not having the specific data available I have used a generic spacing and labels only to give you the idea.没有可用的具体数据,我使用了通用间距和标签,只是为了给你一个想法。 Once you have set the desired position you can generate the sequence of dates associated with it (to generate a sequence of dates see https://www.rdocumentation.org/packages/base/versions/3.6.2/topics/seq.Date )设置所需的 position 后,您可以生成与其关联的日期序列(要生成日期序列,请参阅https://www.rdocumentation.org/packages/base/versions/3.6.2/topics/seq.Date )

axis(1, at=seq(1,40,by=1), label=seq(as.Date("2019-09-27"),as.Date("2020-07-02"),by="week")) # 1 = below axis

You can also change the format of the dates displayed with format() for example label=format(vector_of_date, "%Y-%b-%d") to have year-month(in letter)-day.您还可以更改使用format()显示的日期格式,例如label=format(vector_of_date, "%Y-%b-%d")具有年-月(以字母表示)-日。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM