12

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.

4
  • Can you please put the data somewhere, or add a command to create a mock data.frame so that we can actually help you? Dec 16, 2009 at 15:28
  • 1
    Also, if your x-axis data is of class POSIXct then calling 'axis(side=1,at=tickpos)' should do and is a better idiom than to explicitly force a dispatch. Dec 16, 2009 at 15:29
  • rcs: Hm, maybe I am spoiled from too much zoo and xts. Does it fail even when you use Date instead of the higher-res. POSIXct? Anyway, thanks for the heads-up! Dec 16, 2009 at 15:58
  • @Dirk: yes, also numeric values when Date is used
    – rcs
    Dec 17, 2009 at 7:41

3 Answers 3

14

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

6

Here is a solution that uses zoo simply because that makes the subsetting easy. You could do the same with standard indexing as well:

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

alt text
(source: eddelbuettel.com)

By using par("usr") we avoid the need for explicit values for upper and lower region marks. And the zoo indexing makes finding the start- and endpoints easy. This would work the same way for data in different time resolutions.

1

You can use the chartSeries() function in quantmod with an xts timeSeries and the addTA() function to add the background highlighting:

addTA(xts(rep(TRUE,length(times)), times), on=-1, col="#333333", border=NA)
2
  • For this solution I would have to change my plotting command I guess
    – Peter Smit
    Dec 16, 2009 at 15:04
  • 1
    If you want a specific response, you need to give more detail. What's your plotting command?
    – Shane
    Dec 16, 2009 at 15:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.