| as.layer {latticeExtra} | R Documentation |
Allows overlaying of lattice plots with different scales. The overlaid plots include custom axes and may be drawn in a different style.
Note: in most cases you should use doubleYScale,
and NOT this function.
as.layer(x, ...)
## S3 method for class 'trellis':
as.layer(x, axes = c("x", "y"), opposite = TRUE,
outside = FALSE, ...)
x |
a trellis object. |
axes |
which of the axes to draw (NULL for neither).
Axes might not be drawn anyway, such as if
scales$draw == FALSE. |
opposite |
whether to draw axes on the opposite side to normal: that is, the top and/or right sides rather than bottom and/or left. May be a vector of length 2 to specify for x and y axes separately. |
outside |
whether to draw the axes outside the plot region. Note that space for outside axes will not be allocated automatically. May be a vector of length 2 to specify for x and y axes separately. |
... |
passed to layer: typically
the style argument would be specified. |
Panels from the trellis object x will be drawn in the
corresponding panel of another trellis object, so packet numbers match
(see examples).
Axis setting are taken from the trellis object x, so most
scales arguments such as draw, at, labels etc will
carry over to the overlaid axes. Only the main axis settings are used
(i.e. left or bottom), even when opposite = TRUE.
Currently, outside top axes will be drawn in the strip if there are strips.
an updated trellis object.
Felix Andrews
doubleYScale,
layer,
panel.axis
as.layer, as.layer.trellis
aplot
set.seed(1)
foo <- list(x = as.Date(1:100, "2000-01-01"),
y = cumsum(rnorm(100)))
obj1 <- xyplot(y ~ x, foo, type = "l",
scales = list(format = "%Y %b"))
obj2 <- xyplot(jitter(y, amount = 3) ~ I(x + 360), foo,
type = "l", scales = list(format = "%Y %b"))
## simple case: no axes for the overlaid plot
obj1 + as.layer(obj2, style = 2, axes = NULL)
## draw x axis inside (opposite); remove original ticks from top
update(obj1, scales = list(tck = c(1,0))) +
as.layer(obj2, style = 2, axes = "x")
## draw original axis in that series' style color
col1 <- trellis.par.get("superpose.line")$col[1]
obj1 <- update(obj1, scales = list(tck = c(1,0),
x = list(col = col1, col.line = col1) ))
## draw x axis outside (opposite); need extra space
update(obj1, lattice.options = list(layout.heights =
list(top.padding = list(x = 2)))) +
as.layer(obj2, style = 2, axes = "x", outside = TRUE)
## or draw both series as layers, over a blank "dummy" plot
xAxPad <- list(layout.heights = list(
axis.bottom = list(x = 2.5, units = "char"),
axis.top = list(x = 2.5, units = "char")))
dummy <- update(obj1, panel = function(...) NULL,
scales = list(x = list(draw = FALSE)),
lattice.options = xAxPad)
dummy +
as.layer(obj1, style = 1, axes = "x", out = TRUE, opp = FALSE) +
as.layer(obj2, style = 2, axes = "x", out = TRUE)
## applying one panel layer to several panels of another object
xyplot(Sepal.Length + Sepal.Width ~ Petal.Length + Petal.Width | Species,
data = iris, scales = "free") +
as.layer(levelplot(volcano), under = TRUE)
R version 2.9.0 (2009-04-17)
Copyright (C) 2009 The R Foundation for Statistical Computing
ISBN 3-900051-07-0
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.
R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.
Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.
> library(latticeExtra)
> png(filename="images/as.layer_%03d.png" ,width=480, height=480)
> ### Name: as.layer
> ### Title: Overlay panels with different scales
> ### Aliases: as.layer as.layer.trellis
> ### Keywords: aplot
>
> ### ** Examples
>
> set.seed(1)
> foo <- list(x = as.Date(1:100, "2000-01-01"),
+ y = cumsum(rnorm(100)))
> obj1 <- xyplot(y ~ x, foo, type = "l",
+ scales = list(format = "%Y %b"))
> obj2 <- xyplot(jitter(y, amount = 3) ~ I(x + 360), foo,
+ type = "l", scales = list(format = "%Y %b"))
>
> ## simple case: no axes for the overlaid plot
> obj1 + as.layer(obj2, style = 2, axes = NULL)
>
> ## draw x axis inside (opposite); remove original ticks from top
> update(obj1, scales = list(tck = c(1,0))) +
+ as.layer(obj2, style = 2, axes = "x")
>
> ## draw original axis in that series' style color
> col1 <- trellis.par.get("superpose.line")$col[1]
> obj1 <- update(obj1, scales = list(tck = c(1,0),
+ x = list(col = col1, col.line = col1) ))
>
> ## draw x axis outside (opposite); need extra space
> update(obj1, lattice.options = list(layout.heights =
+ list(top.padding = list(x = 2)))) +
+ as.layer(obj2, style = 2, axes = "x", outside = TRUE)
>
> ## or draw both series as layers, over a blank "dummy" plot
> xAxPad <- list(layout.heights = list(
+ axis.bottom = list(x = 2.5, units = "char"),
+ axis.top = list(x = 2.5, units = "char")))
> dummy <- update(obj1, panel = function(...) NULL,
+ scales = list(x = list(draw = FALSE)),
+ lattice.options = xAxPad)
> dummy +
+ as.layer(obj1, style = 1, axes = "x", out = TRUE, opp = FALSE) +
+ as.layer(obj2, style = 2, axes = "x", out = TRUE)
>
> ## applying one panel layer to several panels of another object
> xyplot(Sepal.Length + Sepal.Width ~ Petal.Length + Petal.Width | Species,
+ data = iris, scales = "free") +
+ as.layer(levelplot(volcano), under = TRUE)
>
>
>
> dev.off()
null device
1
>