【问题标题】:Highlight (shade) plot background in specific time range在特定时间范围内突出显示(阴影)绘图背景
【发布时间】:2010-12-27 06:21:53
【问题描述】:

在一个通用图上,时间在 x 轴上,我想突出显示一些特定年份的时期。

我怎样才能最好地做到这一点?例如,我的想法是在情节后面为突出显示的年份添加一个浅黄色条。

我现在的剧情代码:

pdf("temperature_imfs_big_interm5.pdf", width=6, height=8);
par(mfrow=c(temperature$bigEmdIm5$nimf+1,1), mar=c(2,1,2,1))
for(i in 1:temperature$bigEmdIm5$nimf) {
    plot(timeline$big, temperature$bigEmdIm5$imf[,i], type="l", xlab="", ylab="", ylim=range(temperature$bigEmdIm5$imf[,i]), axes=FALSE, main=paste(i, "-th IMF", sep=""))#; abline(h=0)
  axis.POSIXct(side=1, at=tickpos$big)
}
plot(timeline$big, temperature$bigEmdIm5$residue, xlab="", ylab="", axes=FALSE, main="residue", type="l")
axis.POSIXct(side=1, at=tickpos$big)
dev.off();

其中 temperature$bigEmdIm5 是经验模式分解的输出。数据以月为单位,因此我想突出显示 01/1950 到 12/1950 之间的时间。

【问题讨论】:

  • 你能把数据放在某个地方,或者添加一个命令来创建一个模拟 data.frame 以便我们可以真正帮助你吗?
  • 另外,如果您的 x 轴数据属于 POSIXct 类,那么调用 'axis(side=1,at=tickpos)' 应该可以,并且比显式强制调度更好。跨度>
  • rcs:嗯,也许我被太多zooxts 宠坏了。即使您使用Date 而不是更高分辨率,它是否也会失败。 POSIXct?无论如何,感谢您的提醒!
  • @Dirk: 是的,当使用Date 时也是数值

标签: r plot highlight


【解决方案1】:

您可以使用quantmod 中的chartSeries() 函数与xts timeSeries 和addTA() 函数来添加背景突出显示:

addTA(xts(rep(TRUE,length(times)), times), on=-1, col="#333333", border=NA)

【讨论】:

  • 对于这个解决方案,我猜我必须更改我的绘图命令
  • 如果你想要一个具体的回应,你需要提供更多的细节。你的绘图命令是什么?
【解决方案2】:

使用 alpha 透明度:

x <- seq(as.POSIXct("1949-01-01", tz="GMT"), length=36, by="months")
y <- rnorm(length(x))

plot(x, y, type="l", xaxt="n")
rect(xleft=as.POSIXct("1950-01-01", tz="GMT"),
     xright=as.POSIXct("1950-12-01", tz="GMT"),
     ybottom=-4, ytop=4, col="#123456A0") # use alpha value in col
idx <- seq(1, length(x), by=6)
axis(side=1, at=x[idx], labels=format(x[idx], "%Y-%m"))

或在线条后面绘制突出显示的区域:

plot(x, y, type="n", xaxt="n")
rect(xleft=as.POSIXct("1950-01-01", tz="GMT"),
     xright=as.POSIXct("1950-12-01", tz="GMT"),
     ybottom=-4, ytop=4, col="lightblue")
lines(x, y)
idx <- seq(1, length(x), by=6)
axis(side=1, at=x[idx], labels=format(x[idx], "%Y-%m"))
box()

【讨论】:

    【解决方案3】:

    这是一个使用 zoo 的解决方案,因为这使得子集变得容易。你也可以对标准索引做同样的事情:

    ## create a long monthly sequence and a sub-sequence
    months <- seq( as.Date("1950-01-01"), as.Date("2009-12-12"), by="month")
    subset <- seq( as.Date("1970-01-01"), as.Date("1979-12-31"), by="month")
    
    ## generate some random values
    set.seed(42)
    values <- cumsum(rnorm(length(months)))
    
    ## plot as a zoo object, overlay a gray background and overplot a line in red
    library(zoo)
    Z <- zoo(values, months)
    plot(Z)
    rect(xleft=head(subset,1), xright=tail(subset,1),
         ybottom=par("usr")[3], ytop=par("usr")[4],
         density=NA, col="lightgray")
    lines(Z[subset], col='red')
    box()
    


    (来源:eddelbuettel.com

    通过使用par("usr"),我们避免了对上下区域标记的显式值的需要。 zoo 索引使查找起点和终点变得容易。这对于不同时间分辨率的数据的工作方式相同。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-14
      • 1970-01-01
      • 2010-10-04
      • 1970-01-01
      • 2015-09-08
      • 2021-10-13
      相关资源
      最近更新 更多