R 如何使用autoplot+;使用xts对象自动分层?

R 如何使用autoplot+;使用xts对象自动分层?,r,ggplot2,time-series,xts,R,Ggplot2,Time Series,Xts,我试图使用autoplot中的autolayer功能来区分图片中的预测和测试部分,但我使用的是xts对象。我使用窗口函数分割这两组数据,但是当我使用autoplot中的autolayer函数时,我得到以下错误: 错误:autolayer不支持xts/zoo类型的对象 问题是:是否有可能使用其他方法使用xts对象绘制类似的图形?提前感谢你的帮助 Seriet.xts_train <- window(Seriet.xts, start =

我试图使用
autoplot
中的
autolayer
功能来区分图片中的预测和测试部分,但我使用的是xts对象。我使用窗口函数分割这两组数据,但是当我使用
autoplot
中的
autolayer
函数时,我得到以下错误:

错误:autolayer不支持xts/zoo类型的对象

问题是:是否有可能使用其他方法使用xts对象绘制类似的图形?提前感谢你的帮助

Seriet.xts_train <- window(Seriet.xts, 
                           start = as.Date("2015-01-01"), 
                           end = as.Date("2017-12-31"))
Seriet.xts_train

-2015-01-01 03:00:00 1125
-2015-01-01 04:00:00 1086
-2015-01-01 05:00:00  978
-2015-01-01 06:00:00  947
-2015-01-01 07:00:00 1020
-2015-01-01 08:00:00 1303
-2015-01-01 09:00:00 1557

Seriet.xts_test <- window(Seriet.xts, start = as.Date("2018-01-01"))

Seriet.xts_test

-2018-01-01 03:00:00   55
-2018-01-01 04:00:00   30
-2018-01-01 05:00:00    9
-2018-01-01 06:00:00   13
-2018-01-01 07:00:00   19
-2018-01-01 08:00:00   49
-2018-01-01 09:00:00   43


st.fc.snaive <- snaive(Seriet.xts_train, h=24)

autoplot(st.fc.naive) + autolayer(Seriet.xts_test, series="Conjunto de validación")

Seriet.xts_train如果您想通过将列车点涂成黑色和测试点涂成红色来区分列车和测试,那么:

col <- rep(1:2, c(nrow(train), nrow(test)))
autoplot(rbind(train, test)) + geom_point(col = col)

R中至少有2个自动图层功能,一个在程序包ggplot2中,另一个在预测程序包中

错误可能是您正在使用程序包ggplot2中的程序包

如果正确指定原点,它可能会工作,但仅替换为:
autolayer byforecast::autolayer

这不是一个很好的解决方案,但在最后阶段更改xts对象ts对象的类型可能是一个解决方案

autoplot(ts(a_xts,start=c(2019,4,16),frequency = 
365.25))+autolayer(ts(b_xts,start=c(2019,4,16),frequency = 365.25))

感谢您的欣赏,但问题是结合Snave算法的预测,同时将此预测与xts对象的真实数据(测试数据)进行比较。为此,clasical方法使用autoplot+autolayer,但问题是autolayer函数与xts对象不兼容。那么在这种情况下不可能使用cbind方法。先谢谢你
autoplot(ts(a_xts,start=c(2019,4,16),frequency = 
365.25))+autolayer(ts(b_xts,start=c(2019,4,16),frequency = 365.25))