library("lattice")
library("grid")
# change strip background to levels of grey
# If you like the defaults, this can be omitted.
strip.background <- trellis.par.get("strip.background")
trellis.par.set(strip.background = list(col = grey(7:1/8)))
set.seed(1)
z <- zoo(cbind(a = 1:5, b = 11:15, c = 21:25) + rnorm(5))
# plot a blue running mean on the panel of b.
# Also add a grid.
# We show two ways to do it.
# Number 1. Using trellis.focus.
print(xyplot(z))
trellis.focus("panel", 1, 2, highlight = FALSE)
z.mean <- rollmean(z, 3)
# uncomment next line and remove line after that when
# lattice makes panel.lines generic
# print(panel.lines(time(z.mean), z.mean[,2], col = "blue"))
print(panel.lines.zoo(z.mean[,2], col = "blue"))
print(panel.grid(h = 10, v = 10, col = "grey", lty = 3))
trellis.unfocus()
# Number 2. Using a custom panel routine.
# This example relies on R version 2.40 or higher.
p <- function(x, y, groups = NULL, ...) {
panel.xyplot(x, y, groups = groups, ...)
if (panel.number() == 2) {
panel.lines.zoo(rollmean(zoo(y, x), 3), col = "blue")
panel.grid(h = 10, v = 10, col = "grey", lty = 3)
}
}
print(xyplot(z, panel = panel.plot.custom(panel = p)))
# plot a light grey rectangle "behind" panel b
trellis.focus("panel", 1, 2)
grid.rect(x = 2, w = 1, default.units = "native",
gp = gpar(fill = "light grey"))
do.call("panel.plot.default", trellis.panelArgs())
trellis.unfocus()
# same but make first (i.e. bottom) panel twice as large as others
print(xyplot(z), heights = list(c(2,1,1), units = "null"))
# add a grid - this method does not confine grid to frames
# To do that see prior example.
panel.grid()
# Plot all in one panel.
print(xyplot(z, screens = 1))
# Plot first two columns in first panel and third column in second panel.
# Plot first series using points, second series using lines and third
# series via overprinting both lines and points
# Use colors 1, 2 and 3 for the three series (1=black, 2=red, 3=green)
# Make 2nd (upper) panel 3x the height of the 1st (lower) panel
# Also make the strip background orange.
p <- xyplot(z, screens = c(1,1,2), type = c("p", "l", "o"), col = 1:3,
par.setting = list(strip.background = list(col = "orange")))
print(p, panel.height = list(y = c(1, 3), units = "null"))
# Example of using a custom axis
# Months are labelled with smaller ticks for weeks and even smaller
# ticks for days.
Days <- seq(from = as.Date("2006-1-1"), to = as.Date("2006-8-8"), by = "day")
z <- zoo(seq(length(Days))^2, Days)
Months <- Days[format(Days, "%d") == "01"]
Weeks <- Days[format(Days, "%w") == "0"]
xyplot(z, scales = list(x = list(at = Months)))
trellis.focus("panel", 1, 1, clip.off = TRUE)
panel.axis("bottom", check.overlap = TRUE, outside = TRUE, labels = FALSE,
tck = .7, at = as.numeric(Weeks))
panel.axis("bottom", check.overlap = TRUE, outside = TRUE, labels = FALSE,
tck = .4, at = as.numeric(Days))
trellis.unfocus()
trellis.par.set(strip.background = strip.background)
# separate the panels and suppress the ticks on very top
my.axis <- function(side, ...) if (side != "top") axis.default(side, ...)
my.panel <- function(...) {
panel.axis(outside = TRUE, lab = FALSE)
panel.plot.default(...)
}
set.seed(1)
z <- zoo(cbind(a = 1:5, b = 11:15, c = 21:25) + rnorm(5))
xyplot(z, between = list(x = 1.2, y = 1), par.settings = list(panel = "off"),
axis = my.axis, panel = my.panel)
# left strips but no top strips
xyplot(z, screens = colnames(z), strip = FALSE , strip.left = TRUE)
# same but more complex
xyplot(z, strip = FALSE , strip.left = strip.custom(factor.levels = colnames(z)))
# playwith (>= 0.8-55)
library("playwith")
z3 <- zoo(cbind(a = rnorm(100), b = rnorm(100) + 1), as.Date(1:100))
playwith(xyplot(z3), time.mode = TRUE)
# after running this click on Identify Points and then click on
# points to identify in graph; right click to finish
labs <- paste(z3, index(z3), sep = "@")
playwith(xyplot(z3, type = "o"), labels = labs, label.args = list(cex = 0.7))
# for playwith identify tool this returns indexes into times of clicked points
ids <- do.call(rbind, playDevCur()$ids)$which
z3[ids,]
Run the code above in your browser using DataLab