0

I am trying to build a forecast plot in R. But, inspite of trying many solutions I am unable to plot my X axis in dates.

My data is in the form of :

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

Below is the forecast script I am using:

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)

The output looks like this-- as you can see I have X axis displayed is periods(numbers) rather than in data format. enter image description here

Any help is highly appreciated.

ArunK
  • 1,731
  • 16
  • 35

1 Answers1

0
  • Try to enter explicit specifications in your plot : plot(x=Date, ...)
  • if it does not work try :

    timeline<-seq(from=your.first.date, to=your.last.date, by="week")
    plot(x=...,y=..., xlab=NA, xaxt="n") # no x axis axis.Date(1, at=(timeline), format=F, labels=TRUE) # Special axis

Edit : Sorry for my first solution, which does not fit for your timeserie. The problem is there is no date is time series, but an index refering to "start" and "frequency". Here, your problem comes from your use of "frequency", which is supposed to specify the number of observations by unit of time, ie 4 for quarterly data, 12 for monthly data... Here your unit of time is the week, with 6 open days, that's why your graph axes indicates the index ok the weeks. To have a more readable axis you can try this :

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

You have to use a 7 interval for weeks labels because of the closed day. It's ugly but it works. There is surely a better way looking closely at your ts() to specify those data are daily data, and adapting your forecasting function.

Jean-Noël
  • 368
  • 1
  • 6
  • Hi, how do I decide what 'x = ' in my case. I am trying to say plot (x= Date, y = (Pred) , xlab= NA, xaxt = "n") . But this throws an error saying Date is unrecognisable. – smruthi gupta Aug 09 '16 at 22:59