Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
449 views
in Technique[技术] by (71.8m points)

r - Highlight (shade) plot background in specific time range

On a generic plot, with time on the x-axis, I would like to highlight a period of some specific years.

How can I bestly do this? My idea is for example a light yellow bar for the highlighted years, behind the plot of course.

The plot code I have now:

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();

Where temperature$bigEmdIm5 is the output of emperical mode decompostion. The data is in months, so I would like to higlight 01/1950 until 12/1950 for example.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Using alpha transparency:

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"))

enter image description here

or plot highlighted region behind lines:

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()

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...