Learn R Programming

grid (version 3.3.1)

legendGrob: Constructing a Legend Grob

Description

Constructing a legend grob (in progress)

Usage

legendGrob(labels, nrow, ncol, byrow = FALSE, do.lines = has.lty || has.lwd, lines.first = TRUE, hgap = unit(1, "lines"), vgap = unit(1, "lines"), default.units = "lines", pch, gp = gpar(), vp = NULL)
grid.legend(..., draw=TRUE)

Arguments

labels
legend labels (expressions)
nrow, ncol
integer; the number of rows or columns, respectively of the legend “layout”. nrow is optional and typically computed from the number of labels and ncol.
byrow
logical indicating whether rows of the legend are filled first.
do.lines
logical indicating whether legend lines are drawn.
lines.first
logical indicating whether legend lines are drawn first and hence in a plain “below” legend symbols.
hgap
horizontal space between the legend entries
vgap
vertical space between the legend entries
default.units
default units, see unit.
pch
legend symbol, numeric or character, passed to pointsGrob(); see also points for interpretation of the numeric codes.
gp
an R object of class gpar, typically the output from a call to the function gpar, is basically a list of graphical parameter settings.
vp
a Grid viewport object (or NULL).
...
for grid.legend(): all the arguments above are passed to legendGrob().
draw
logical indicating whether graphics output should be produced.

Value

Both functions create a legend grob (a graphical object describing a plot legend), but only grid.legend draws it (only if draw is TRUE).

See Also

Grid, viewport; pointsGrob, linesGrob.

grid.plot.and.legend contains a simple example.

Examples

Run this code
  ## Data:
  n <- 10
  x <- stats::runif(n) ; y1 <- stats::runif(n) ; y2 <- stats::runif(n)
  ## Construct the grobs :
  plot <- gTree(children=gList(rectGrob(),
                  pointsGrob(x, y1, pch=21, gp=gpar(col=2, fill="gray")),
                  pointsGrob(x, y2, pch=22, gp=gpar(col=3, fill="gray")),
                  xaxisGrob(),
                  yaxisGrob()))
  legd <- legendGrob(c("Girls", "Boys", "Other"), pch=21:23,
                     gp=gpar(col = 2:4, fill = "gray"))
  gg <- packGrob(packGrob(frameGrob(), plot),
                 legd, height=unit(1,"null"), side="right")

  ## Now draw it on a new device page:
  grid.newpage()
  pushViewport(viewport(width=0.8, height=0.8))
  grid.draw(gg)

Run the code above in your browser using DataLab