r - 如何用多边形突出显示时间序列预测的置信区域

标签 r plot time-series polygon confidence-interval

我正在尝试使用两条蓝线之间的多边形函数为时间序列图着色,如下所示。

我的代码如下: enter image description here

生成上图的代码如下:

set.seed(2)
Original <- ts(na.omit(rnorm(1:96,mean=20, sd=1)), frequency = 12, start=c(2008,1))
upper <- ts(na.omit(rnorm(1:96,mean=30, sd=1)), frequency = 12, start=c(2015,1),end=c(2015,12))
lower <- ts(na.omit(rnorm(1:96,mean=10, sd=1)), frequency = 12, start=c(2015,1),end=c(2015,12))
predict <- ts(na.omit(rnorm(1:96,mean=18, sd=1)), frequency = 12, start=c(2015,1),end=c(2015,12))

par(mfrow=c(1,1))
plot(Original,type="l",col="gray", ylim=c(0,50))
lines(upper,col="blue")
lines(lower,col="blue")
lines(predict,col="red", lwd="2")               

我目前使用的多边形函数如下:

polygon(x= c(tmp,rev(tmp)),y= c(upper,rev(lower)), col="grey", border=NA)

上面的代码很好,除了我的 X 坐标。我无法指定 X 坐标,因为它是一个时间序列。时间序列是每月的。蓝线从 2015 年 1 月开始,到 2015 年 12 月结束。

有没有办法在 vector/matrix/date.frame 中指定时间序列的“X”坐标以显示 2015 年 1 月到 2015 年 12 月?

我的目标:我想为两条蓝线之间的区域着色,并且只为两条蓝线之间的区域着色。因此,使用多边形。

最佳答案

好的,您的问题真的是:如何在调用 polygon 时获取变量 tmp 的适当值。

现在,简单地尝试一下:

tmp <- seq(from = 2015, by = 1/12, length = 12)
polygon(x= c(tmp,rev(tmp)),y= c(upper,rev(lower)), col="grey", border=NA)

enter image description here

但是你可以自己做,使情节看起来更好,例如更改线条颜色,线条宽度。此外,您可能需要考虑先绘制多边形,然后叠加时间序列图。

关于r - 如何用多边形突出显示时间序列预测的置信区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37547509/

相关文章:

r - 在 dplyr 的 group_by 中使用对象而不是列名

r - 为什么 'with' 不通过嵌套函数传递变量范围?

R Shiny 错误 : "Object of Type Closer is not Subsettable"

r - 在 R : how to highlight specific areas of interest? 中抛光蜘蛛网图

R函数用阴影绘制不等式

python - Pandas Dataframe 按时间序列进行透视和重新索引

matlab - Octave 时间序列移动平均线

sql - 聚合时间序列的高效子查询

r - R 中的词干提取

python - 为分组列创建直方图