【问题标题】:How to plot a fringe in zoo object time series to highlight a period?如何在动物园对象时间序列中绘制边缘以突出显示一个时期?
【发布时间】:2021-01-25 22:09:03
【问题描述】:
library(zoo)
     dat<-data.frame(
          prec<-rnorm(650,mean=300),
          temp<-rnorm(650,mean = 22),
          pet<-rnorm(650,mean = 79),
          bal<-rnorm(650,mean = 225))
        colnames(dat)<-c("prec","temp","pet","bal")
        
        dat<-ts(dat,start = c(1965,1),frequency = 12)
        plot.zoo(dat)
        
        rect(xleft=1975,xright = 1982,ybottom=0,ytop=800,col= '#FFFF0022',border = "transparent")
        rect(xleft=1990,xright = 2000,ybottom=0,ytop=800,col= '#00BFFF22',border = "transparent")
        rect(xleft=2010,xright = 2015,ybottom=0,ytop=500,col= '#FF000022',border = "transparent")

但我只能得到一些超出边界或不在正确 x 轴上的东西 This is my result so far

【问题讨论】:

  • 我无法重现这个。您的 rect 对象在我的系统上的正确位置
  • 抱歉,代码中存在错误,我已更正。请重试,感谢您的帮助

标签: r time-series zoo


【解决方案1】:

使用面板功能。同样使用xblocks 可以进一步简化:

plot.zoo(dat, panel = function(x, y, ...) {
  lines(x, y, ...)
  year <- as.integer(x)
  xblocks(x, year %in% 1975:1982, col = '#FFFF0022')
  xblocks(x, year %in% 1990:2000, col = '#00BFFF22')
  xblocks(x, year %in% 2010:2015, col = '#FF000022')
})

【讨论】:

    猜你喜欢
    • 2013-05-15
    • 1970-01-01
    • 2013-06-13
    • 2012-02-27
    • 1970-01-01
    • 2012-02-27
    • 1970-01-01
    • 2017-10-14
    • 2021-03-05
    相关资源
    最近更新 更多