
Last chance! 50% off unlimited learning
Sale ends in
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)
nrow
is optional and
typically computed from the number of labels and ncol
.unit
.pointsGrob()
; see also points
for
interpretation of the numeric codes.gpar
, typically the output
from a call to the function gpar
, is basically a list of
graphical parameter settings.viewport
object (or NULL
).grid.legend()
: all the arguments above are
passed to legendGrob()
.grob
(a graphical object
describing a plot legend), but only grid.legend
draws it (only
if draw
is TRUE
).
viewport
;
pointsGrob
, linesGrob
. grid.plot.and.legend
contains a simple example.
## 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