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.
Using alpha transparency:
or plot highlighted region behind lines:
Here is a solution that uses zoo simply because that makes the subsetting easy. You could do the same with standard indexing as well:
alt text http://dirk.eddelbuettel.com/misc/plotRegionExample.png
By using
par("usr")
we avoid the need for explicit values for upper and lower region marks. And thezoo
indexing makes finding the start- and endpoints easy. This would work the same way for data in different time resolutions.You can use the
chartSeries()
function inquantmod
with anxts
timeSeries and theaddTA()
function to add the background highlighting: