Learn R Programming

lattice (version 0.9-13)

xyplot: Common Bivariate Trellis Plots

Description

These are the most commonly used Trellis functions to look at pairs of variables. By far the most common is xyplot, designed mainly for two continuous variates (though factors can be supplied as well, in which case they will simply be coerced to numeric), which produces Conditional Scatterplots. The others are useful when one of the variates is a factor or a shingle. See details below.

Most of the arguments documented here are also applicable for many of the other Trellis functions. These are not described in any detail elsewhere, and this should be considered the canonical documentation for such arguments.

Note that any arguments passed to these functions and not recognized by them will be passed to the panel function. Most predefined panel functions have arguments that customize its output. These arguments are described only in the help pages for these panel functions, but can usually be supplied as arguments to the high level plot.

Usage

xyplot(formula,
       data = parent.frame(),
       panel = if (is.null(groups)) "panel.xyplot"
               else "panel.superpose",
       allow.multiple,
       outer,
       aspect = "fill",
       as.table = FALSE,
       between,
       groups,
       key,
       auto.key = FALSE,
       legend,
       layout,
       main,
       page,
       par.strip.text,
       prepanel,
       scales,
       skip,
       strip = "strip.default",
       sub,
       xlab,
       xlim,
       ylab,
       ylim,
       drop.unused.levels = TRUE,
       par.settings,
       perm.cond,
       index.cond,
       ...,
       panel.groups = "panel.xyplot",
       subscripts,
       subset)
dotplot(formula,
        data, 
        panel = "panel.dotplot",
        groups = NULL,
        ...,
        subset = TRUE)
barchart(formula,
         data,
         panel = "panel.barchart",
         box.ratio = 2,
         groups = NULL,
         ...,
         subset = TRUE)
stripplot(formula,
          data,
          panel = "panel.stripplot",
          jitter = FALSE,
          factor = .5,
          box.ratio = if (jitter) 1 else 0,
          groups = NULL,
          ...,
          subset = TRUE)
bwplot(formula,
       data,
       panel = "panel.bwplot",
       box.ratio = 1,
       ...,
       horizontal,
       subset = TRUE)

Arguments

formula
a formula describing the form of conditioning plot. The formula is generally of the form y ~ x | g1 * g2 * ..., indicating that plots of y (on the y axis) versus x (on the x axis) should be produced condi
data
a data frame containing values for any variables in the formula, as well as groups and subset if applicable. By default the environment where the function was called from is used.
allow.multiple, outer
logical flags to control what happens with formulas like y1 + y2 ~ x. See the entry for formula for details. allow.multiple defaults to TRUE whenever it makes sense, and outer
box.ratio
applicable to bwplot, barchart and stripplot, specifies the ratio of the width of the rectangles to the inter rectangle space.
horizontal
logical, applicable to bwplot, dotplot, barchart and stripplot. Determines which of x and y is to be a factor or shingle (y if TRUE, x otherwise). Defaults to
jitter
logical specifying whether the values should be jittered by adding a random noise in stripplot.
factor
numeric controlling amount of jitter. Inverse effect compared to S ?
panel
Once the subset of rows defined by each unique combination of the levels of the grouping variables are obtained (see above), the corresponding x and y variables (or some other variables, as appropriate, in the case of
panel.groups
useful mostly for xyplot and densityplot. Applies when panel is panel.superpose (which happens by default in these cases if groups is non-null)
aspect
controls physical aspect ratio of the panels (same for all the panels). It can be specified as a ratio (vertical size/horizontal size) or as a character string. Legitimate values are "fill" (the default) which tries to make the panels as
as.table
logical that controls the order in which panels should be plotted: if FALSE, panels are drawn left to right, bottom to top (graph), if TRUE, left to right, top to bottom (matrix).
between
a list with components x and y (both usually 0 by default), numeric vectors specifying the space between the panels (units are character heights). x and y are repeated to account for all pane
groups
used typically with panel=panel.superpose to allow display controls (color, lty etc) to vary according to a grouping variable. Formally, if groups is specified, then groups along with subscripts is passed
auto.key
A logical (indicating whether a key is to be drawn automatically when a grouping variable is present in the plot), or a list of parameters that would be valid arguments to simpleKey. If
key
A list of arguments that define a legend to be drawn on the plot. This list is used as an argument to the draw.key function, which produces a grid object eventually plotted by the print method
legend
the legend argument can be useful if one wants to place more than one key. It also allows one to use arbitrary ``grob''s (grid objects) as legends. If used, legend must be a list with an arbitrary number of components. Ea
layout
In general, a Trellis conditioning plot consists of several panels arranged in a rectangular array, possibly spanning multiple pages. layout determines this arrangement.

layout is a numeric vector giving the number o

main
character string or expression or list describing main title to be placed on top of each page. Defaults to NULL. Can be a character string or expression, or a list with components label, cex, col, font. The labe
page
a function of one argument (page number) to be called after drawing each page. The function must be `grid-compliant', and is called with the whole display area as the default viewport.
par.strip.text
list of graphical parameters to control the strip text, possible components are col, cex, font, lines. The first three control graphical parameters while the last is a means of altering the height of the strips. This can be useful
prepanel
function that takes the same arguments as the panel function and returns a list containing four components xlim, ylim, dx, dy. If xlim and ylim are not explicitly specified (possibly as compo
scales
list determining how the x- and y-axes (tick marks and labels) are drawn. The list contains parameters in name=value form, and may also contain two other lists called x and y of the same form (described below). Compon
skip
logical vector (default FALSE), replicated to be as long as the number of panels (spanning all pages). If TRUE, nothing is plotted in the corresponding panel. Useful for arranging plots in an informative manner.
strip
logical flag or function. If FALSE, strips are not drawn. Otherwise, strips are drawn using the strip function, which defaults to strip.default. See documentation of strip.default to see the
sub
character string or expression for a subtitle to be placed at the bottom of each page. See entry for main for finer control options.
subscripts
logical specifying whether or not a vector named subscripts should be passed to the panel function. Defaults to FALSE, unless groups is specified, or if the panel function accepts an argument named subscripts. (One sh
subset
logical or integer indexing vector (can be specified in terms of variables in data). Everything will be done on the data points indexed by subset. In case subscripts is TRUE, the subscripts will provide i
xlab
character string or expression giving label for the x-axis. Defaults to the expression for x in formula. Specify as NULL to omit the label altogether. Fine control is possible, see entry for sub
xlim
Normally a numeric vector of length 2 (possibly a DateTime object) giving minimum and maximum for the x-axis, or, a character vector, expected to denote the levels of x. The latter form is interpreted as a range containing c(1, le
ylab
character string or expression giving label for the y-axis. Defaults to the expression for y in formula. Fine control possible, see entry for xlab.
ylim
same as xlim, applied to the y-axis.
drop.unused.levels
logical indicating whether the unused levels of factors will be dropped. Defaults to TRUE, but it is sometimes useful to suppress dropping to preserve an useful layout. For finer control, this argument could also be list containin
par.settings
a list that could be supplied to lset. This enables the user to attach some display settings to the trellis object itself rather than change the settings globally. When the object is printed, these
perm.cond
numeric vector, a permutation 1:n, where n is the number of conditioning variables. The order in which panels are drawn depends on the order of the conditioning variables specified in the formula.
index.cond
While perm.cond permutes the dimensions of the multidimensional array of panels, index.cond can be used to subset that array.

The panel display order within each conditioning variable depends on the order of thei

...
other arguments, passed to the panel function.

The arguments horizontal and panel.groups are documented here to avoid confusion, but they are actually not recognised by these high level functions. Rather, they are pa

Value

  • An object of class ``trellis''. The `update' method can be used to update components of the object and the `print' method (usually called by default) will plot it on an appropriate plotting device.

synopsis

xyplot(formula, data = parent.frame(), allow.multiple = is.null(groups) || outer, outer = FALSE, auto.key = FALSE, aspect = "fill", panel = if (is.null(groups)) "panel.xyplot" else "panel.superpose", prepanel = NULL, scales = list(), strip = TRUE, groups = NULL, xlab, xlim, ylab, ylim, drop.unused.levels = TRUE, ..., subscripts = !is.null(groups), subset = TRUE) bwplot(formula, data = parent.frame(), allow.multiple = is.null(groups) || outer, outer = FALSE, auto.key = FALSE, aspect = "fill", panel = "panel.bwplot", prepanel = NULL, scales = list(), strip = TRUE, groups = NULL, xlab, xlim, ylab, ylim, box.ratio = 1, horizontal = NULL, drop.unused.levels = TRUE, ..., subscripts = !is.null(groups), subset = TRUE)

Details

These are for the most part decriptions generally applicable to all high level Lattice functions, with special emphasis on xyplot, bwplot etc. For other functions, their individual documentation should be studied in addition to this.

See Also

Lattice, print.trellis, shingle, banking, reshape panel.xyplot, panel.bwplot, panel.barchart, panel.dotplot, panel.stripplot, panel.superpose, panel.loess, panel.linejoin, strip.default, simpleKey lset

Examples

Run this code
require(stats)
## Tonga Trench Earthquakes
data(quakes)
Depth <- equal.count(quakes$depth, number=8, overlap=.1)
xyplot(lat ~ long | Depth, data = quakes)

## Examples with data from `Visualizing Data' (Cleveland)
## (obtained from Bill Cleveland's Homepage :
## http://cm.bell-labs.com/cm/ms/departments/sia/wsc/, also
## available at statlib)
data(ethanol)
EE <- equal.count(ethanol$E, number=9, overlap=1/4)
## Constructing panel functions on the fly; prepanel
xyplot(NOx ~ C | EE, data = ethanol,
       prepanel = function(x, y) prepanel.loess(x, y, span = 1),
       xlab = "Compression Ratio", ylab = "NOx (micrograms/J)",
       panel = function(x, y) {
           panel.grid(h=-1, v= 2)
           panel.xyplot(x, y)
           panel.loess(x,y, span=1)
       },
       aspect = "xy")



## with and without banking

data(sunspots)
spots <- by(sunspots, gl(235, 12, lab = 1749:1983), mean)
plot <- xyplot(spots ~ 1749:1983, xlab = "", type = "l",
               scales = list(x = list(alternating = 2)),
               main = "Average Yearly Sunspots")
print(plot, position = c(0, .3, 1, .9), more = TRUE)
print(update(plot, aspect = "xy", main = "", xlab = "Year"),
      position = c(0, 0, 1, .3))




## Multiple variables in formula for grouped displays

data(iris)
xyplot(Sepal.Length + Sepal.Width ~ Petal.Length + Petal.Width | Species, 
       data = iris, scales = "free", layout = c(2, 2),
       auto.key = list(x = .6, y = .7, corner = c(0, 0)))


## user defined panel functions

data(state)
states <- data.frame(state.x77,
                     state.name = dimnames(state.x77)[[1]], 
                     state.region = state.region) 
xyplot(Murder ~ Population | state.region, data = states, 
       groups = state.name, 
       panel = function(x, y, subscripts, groups)  
       ltext(x = x, y = y, label = groups[subscripts], cex=1,
             fontfamily = "HersheyPlain"))

data(barley)
barchart(yield ~ variety | site, data = barley,
         groups = year, layout = c(1,6),
         ylab = "Barley Yield (bushels/acre)",
         scales = list(x = list(abbreviate = TRUE,
                       minlength = 5)))
barchart(yield ~ variety | site, data = barley,
         groups = year, layout = c(1,6), stack = TRUE, 
         auto.key = list(points = FALSE, rectangles = TRUE, space = "right"),
         ylab = "Barley Yield (bushels/acre)",
         scales = list(x = list(rot = 45)))

data(singer)
bwplot(voice.part ~ height, data=singer, xlab="Height (inches)")
dotplot(variety ~ yield | year * site, data=barley)

dotplot(variety ~ yield | site, data = barley, groups = year,
        key = simpleKey(levels(barley$year), space = "right"),
        xlab = "Barley Yield (bushels/acre) ",
        aspect=0.5, layout = c(1,6), ylab=NULL)

stripplot(voice.part ~ jitter(height), data = singer, aspect = 1,
          jitter = TRUE, xlab = "Height (inches)")
## Interaction Plot
data(OrchardSprays)
bwplot(decrease ~ treatment, OrchardSprays, groups = rowpos,
       panel = "panel.superpose",
       panel.groups = "panel.linejoin",
       xlab = "treatment",
       key = list(lines = Rows(trellis.par.get("superpose.line"),
                  c(1:7, 1)), 
                  text = list(lab = as.character(unique(OrchardSprays$rowpos))),
                  columns = 4, title = "Row position"))

Run the code above in your browser using DataLab