zoo (version 1.5-4)

xyplot.zoo: Plot zoo Series with Lattice

Description

xyplot methods for time series objects (of class "zoo" or "ts", "its"). These functions are still under development and the interface and functionality might be modified/extended in future releases.

Usage

## S3 method for class 'zoo':
xyplot(x, data, screens = seq(length = NCOL(x)), 
    default.scales = list(y = list(relation = "free")), 
    layout = NULL, xlab = "Index", ylab = NULL, 
    lty = trellis.par.get("plot.line")$lty,
    lwd = trellis.par.get("plot.line")$lwd,
    pch = trellis.par.get("plot.symbol")$pch, 
    type = "l", 
    col = trellis.par.get("plot.line")$col,
    strip = TRUE, panel = panel.plot.default, ...) 

panel.plot.default(x, y, subscripts, groups, 
    panel = panel.xyplot,
    col = 1, type = "p", pch = 20, lty = 1, lwd = 1, ...) 

panel.plot.custom(...) 

panel.lines.zoo(x, ...)
panel.points.zoo(x, ...)
panel.segments.zoo(x0, x1, ...) 
panel.text.zoo(x, ...)
panel.rect.zoo(x0, x1, ...)
panel.arrows.zoo(x0, x1, ...)
panel.polygon.zoo(x, ...)

Arguments

x, x0, x1
time series object of class "zoo", "ts" or "its". For panel.plot.default it should be a numeric vector.
y
numeric vector or matrix.
subscripts, groups, panel
arguments for panel functions, see description of panel argument in xyplot.
data
currently not used.
screens
factor (or coerced to factor) whose levels specify which graph each series is to be plotted in. screens = c(1, 2, 1) would plot series 1, 2 and 3 in graphs 1, 2 and 1.
default.scales
scales specification. The default is set so that all series have the "same" X axis but "free" Y axis. See xyplot in the lattice package for
layout
numeric vector of length 2 specifying number of columns and rows in the plot, see xyplot for more details. The default is to fill columns with up to 5 rows.
xlab
character string used as the X axis label.
ylab
character string used as the Y axis label. If there are multiple panels it may be a character vector the same length as the number of panels.
lty, lwd, pch, type, col
graphical arguments passed to xyplot. These arguments can also be vectors or (named) lists, see details for more information.
strip
logical, character or function specifying headings used for panels. If character, should be a vector the same length as the number of panels. If TRUE column names are used for headers. If FALSE, no headings
...
additional arguments passed to xyplot.

Value

  • Invisibly returns a "trellis" class object. Printing this object using print will display it.

Details

xyplot.zoo plots a "zoo", "ts" or "its" object using xyplot from lattice. Series of other classes are coerced to "zoo" first. The handling of several graphical parameters is more flexible for multivariate series. These parameters can be vectors of the same length as the number of series plotted or are recycled if shorter. They can also be (partially) named list, e.g., list(A = c(1,2), c(3,4)) in which c(3, 4) is the default value and c(1, 2) the value only for series A. The screens argument can be specified in a similar way. plot.panel.default is the default panel function. plot.panel.custom facilitates the development of custom panels. Usually it has one argument "panel" which specifies the custom panel. That panel typically calls plot.panel.default. The panel function may use the panel.number() function to find out which panel is currently being executed. See the examples.

See Also

zoo, plot.ts, barplot, plot.zoo

Examples

Run this code
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 DataCamp Workspace