I would like to change the x-axis when plotting some forecasts. The model is daily updated via crontab. The x-scale must include that daily increase in date:
Example data:
# where dates changes according to Sys.Date-1
dates <- seq(as.Date("2015-01-01"), Sys.Date()-1, by = "days")
# where x is updated daily
x <- diffinv(rnorm(length(dates)-1))
df<-data.frame(dates,x)
# split data and train model
df$x<-as.ts(df$x)
# required libraries
library(caret)
library(forecast)
library(plyr)
# the time series is updated on daily basis
date1 <- strptime("2016-02-04", format="%Y-%m-%d")
date2 <- strptime(Sys.time()-1, format="%Y-%m-%d")
date3<-difftime(date2,date1,units="days")
# here I split data into time and test data according to initialWindow "2016-02-04"
timeSlices <- createTimeSlices(1:nrow(df),
initialWindow = 400, horizon = date3, fixedWindow = TRUE)
#extract data for fitting the model
trainSlices <- timeSlices[[1]]
testSlices <- timeSlices[[2]]
# here I calculate the fit and forecast
fit <- tbats(df[trainSlices[[1]],]$x, seasonal.periods=c(7,365), use.trend=TRUE, use.parallel=TRUE)
pred <- forecast(fit,h=length(df[testSlices[[1]],]$x))
# here I plot actual vs. predicted values
plot(forecast(fit,h=length(df[testSlices[[1]],]$x)),ylab = "x ",xlab="Daily units from 2015-01-01 to CurrentDate", main="Forecast", include=30)
lines(x = as.numeric(rownames(df[testSlices[[1]],])), df[testSlices[[1]],]$x, col = "red")
legend(x = "topleft", legend = c("Prediction", "Actual Data"), col = c("blue", "red"), lty = c(1, 1))
I would like to change the daily units into dates scale
aka 2015-07-01
and so on, which is also reflected in the daily update. I tried to use xaxt="n"
and adding dates scale
with Sys.Date()-1
:
dates <- seq(Sys.Date()-30, Sys.Date()-1, by = "days")
plot(xaxt="n",forecast(fit,h=length(df[testSlices[[1]],]$x)),ylab = "x ",xlab="Daily units from 2015-01-01 to CurrentDate", main="Forecast", include=30)
lines(x = as.numeric(rownames(df[testSlices[[1]],])), df[testSlices[[1]],]$x, col = "red")
legend(x = "topleft", legend = c("Prediction", "Actual Data"), col = c("blue", "red"), lty = c(1, 1))
# here I wanted to add the new scale
axis(1, at = dates,labels = TRUE)
But that doesn't plot the x-scale.
The reason why your axis()
doesn't show is because the include =30
only shows the 30 last elements in the window, but actually the axis starts at 1.
If you slide your axis to the current window then it will show:
plot(xaxt="n",forecast(fit,h=length(df[testSlices[[1]],]$x-1)),ylab = "x ",xlab="Daily units from 2015-01-01 to CurrentDate", main="Forecast", include=30)
lines(x = as.numeric(rownames(df[testSlices[[1]],])), df[testSlices[[1]],]$x, col = "red")
legend(x = "topleft", legend = c("Prediction", "Actual Data"), col = c("blue", "red"), lty = c(1, 1))
at <- seq(1,as.integer(date3),length.out=4)
dates <- seq(date1, date2, by="days")[at]
axis(1, at = at+400, labels = dates)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With