R 当范围每天变化时,如何使用plot()调整x轴?

R 当范围每天变化时,如何使用plot()调整x轴?,r,plot,R,Plot,我想在绘制一些预测时更改x轴。该模型通过crontab每日更新。x刻度必须包括日期的每日增加: 示例数据: # 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

我想在绘制一些预测时更改x轴。该模型通过crontab每日更新。x刻度必须包括日期的每日增加:

示例数据:

# 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))

但这不会绘制x比例。

轴()之所以不显示,是因为
include=30
仅显示窗口中最后30个元素,但实际上轴从1开始

如果将轴滑动到当前窗口,它将显示:

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)  
plot(xaxt=“n”,预测(拟合,h=length(df[testSlices[[1]],]$x-1)),ylab=“x”,xlab=“从2015-01-01到当前日期的每日单位”,main=“预测”,包括=30)
行(x=as.numeric(行名(df[testSlices[[1]],])),df[testSlices[[1]],]$x,col=“red”)
图例(x=“左上角”,图例=c(“预测”,“实际数据”),col=c(“蓝色”,“红色”),lty=c(1,1))

至少,您是仅限于基本绘图还是也可以使用
ggplot
解决方案?我不受限制。我之所以使用base,是因为
库(预测)
不支持
ggplot
,至少我不能使用它。另请参见和。
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)