latticeExtra (version 0.6-9)

panel.xblocks: Plot contiguous blocks along x axis.

Description

Plot contiguous blocks along x axis. A typical use would be to highlight events or periods of missing data.

Usage

panel.xblocks(x, ...)

## S3 method for class 'default':
panel.xblocks(x, y, ..., gaps = FALSE,
              height = unit(1, "npc"),
              block.y = unit(0, "npc"), vjust = 0,
              col = NULL, border = NA, name = "xblocks",
              last.step = median(diff(tail(x))))

## S3 method for class 'ts':
panel.xblocks(x, y = NULL, ...)

Arguments

x, y
In the default method, x gives the ordinates along the x axis and must be in increasing order. y gives the colour values to plot as contiguous blocks. If y is numeric, data coverage is plotted, by conv
...
In the default method, further arguments are graphical parameters passed on to gpar.
gaps
highlights missing values in the data only. This is similar to passing y = is.na(y), but one cannot do that to a time series object without losing the time frame attributes.
height
height of blocks, defaulting to the full panel height. Numeric values are interpreted as native units.
block.y
y axis position of the blocks. Numeric values are interpreted as native units.
vjust
vertical justification of the blocks relative to block.y. See grid.rect.
col
if col is specified, it determines the colors of the blocks defined by y. If multiple colours are specified they will be repeated to cover the total number of blocks.
border
border colour.
name
a name for the grob (grid object).
last.step
width (in native units) of the final block. Defaults to the median of the last 5 time steps (assuming steps are regular).

Details

Blocks are drawn forward in "time" from the specified x locations, up until the following value. Contiguous blocks are calculated using rle.

See Also

xyplot.ts, panel.rect, grid.rect

Examples

Run this code
## 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