## Example of highlighting peaks in a time series.
set.seed(0)
flow <- ts(filter(rlnorm(200, mean = 1), 0.8, method = "r"))
## using an explicit panel function
xyplot(flow, panel = function(x, y, ...) {
panel.xblocks(x, y > mean(y), col = "lightgrey")
panel.xyplot(x, y, ...)
})
## using layers; this is the `ts` method because `>` keeps it as ts.
xyplot(flow) +
layer_(panel.xblocks(flow > mean(flow), col = "lightgrey"))
## Example of alternating colours, here showing calendar months
flowdates <- Sys.Date() + as.vector(time(flow))
xyplot(flow) +
layer_(panel.xblocks(time(flow), months(flowdates),
col = c("lightgrey", "#e6e6e6"), border = "darkgrey"))
## Example of highlighting gaps (NAs) in time series.
## set up example data
z <- ts(matrix(1:18-1, ncol = 3))
colnames(z) <- c("A","B","C")
z[3:4, "B"] <- NA
z[1, "C"] <- NA
z
## show data coverage only (highlighting gaps)
xyplot(z, panel = panel.xblocks,
scales = list(y = list(draw = FALSE)))
## draw gaps in red
xyplot(z, type = c("p","s")) +
layer_(panel.xblocks(x, y, gaps = TRUE, col = "red"))
## Example of overlaying blocks from a different series.
## Are US presidential approval ratings linked to sunspot activity?
## Set block height, default justification is along the bottom.
xyplot(presidents) + layer(panel.xblocks(sunspot.year > 50, height = 3))
## Multiple colour values given in the 'y' argument.
sscols <- cut(sunspot.year, c(50,150,Inf), labels=c("yellow","orange"))
xyplot(presidents, lwd = 2) +
layer_(panel.xblocks(time(sunspot.year), y = sscols))
Run the code above in your browser using DataLab