## generate a random time series object with 12 columns
set.seed(1)
dat <- ts(matrix(cumsum(rnorm(200 * 12)), ncol = 12))
colnames(dat) <- paste("series", LETTERS[1:12])
## show simple line plot first, for reference.
xyplot(dat, scales = list(y = "same"))
## horizonplot:
## draw each series with a different baseline;
## ('origin' default is the first data value in each series).
## This highlights deviations within each series.
horizonplot(dat, layout = c(1,12))
## now use same scales in each panel
## (so colors represent common ranges across all panels).
## This highlights differences across series.
## One could potentially draw a colorkey in this case.
horizonplot(dat, scales = list(y = list(relation = "same")),
origin = 0, layout = c(1,12)) +
layer(panel.scaleArrow(x = .99, pos = 2))
## illustration of the cut points used in the following plot
xyplot(EuStockMarkets, scales = list(y = "same"),
panel = function(x, y, ...) {
rgn <- trellis.par.get("regions")$col
col <- rgn[1 + (length(rgn)-1) * 0:7/7]
for (i in c(-4:-1, 3:0)) {
if (i >= 0)
yi <- pmax(4000, pmin(y, 4000 + 1000 * (i+1)))
if (i < 0)
yi <- pmin(4000, pmax(y, 4000 + 1000 * i))
panel.xyarea(x, yi, origin = 4000,
col = col[i+5], border = NA)
}
panel.lines(x, y)
panel.abline(h = 4000, lty = 2)
})
## compare with previous plot
horizonplot(EuStockMarkets, strip.left = TRUE,
scales = list(y = list(relation = "same")), ylim = c(4000, 5000)) +
layer(panel.scaleArrow(x = 0.99, col = "grey", srt = 90, cex = 0.75))
.featured_example <- TRUE
## a cut-and-stack plot; should use "same" y scales!
horizonplot(sunspots, cut = list(n = 23, overlap = 0),
scales = list(y = list(relation = "same"), draw = FALSE),
origin = 100, strip.left = FALSE, layout = c(1,23)) +
layer(grid::grid.text(round(x[1]), x = 0, just = "left"))
Run the code above in your browser using DataLab