在特定时间范围内突出显示(阴影)绘图背景

Pet*_*mit 10 plot r highlight

在通用图上,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();
Run Code Online (Sandbox Code Playgroud)

温度$ bigEmdIm5是经过模式分解的输出.数据是几个月,所以我想高亮01/1950,直到12/1950.

rcs*_*rcs 12

使用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"))
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

或者在线后面突出显示区域:

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()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述


Dir*_*tel 5

这是一个使用动物园的解决方案,因为这使得子集很容易.您也可以使用标准索引执行相同的操作:

## 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()
Run Code Online (Sandbox Code Playgroud)

alt text http://dirk.eddelbuettel.com/misc/plotRegionExample.png

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