legend.dimensions | R Documentation |
This function is used to determine the size of a legend as it appears on a plot.
legend.dimensions(expr, envir = parent.frame(),
enclos = if (is.list(envir) || is.pairlist(envir))
parent.frame() else baseenv(),
trace = FALSE)
expr |
a |
envir |
the |
enclos |
Relevant when |
trace |
Should |
expr
would usually be a call to
graphics::legend
, though anything that adds a
legend to a plot and returns identical output to graphics::legend
is
accepted.
If expr
is a formula, it should be of the form ~call( .. )
where call( .. )
is the call to be evaluated (the call that produces
the legend when evaluated). The environment
of
expr
will be used instead of envir
, and enclos
will be
ignored.
A list with components:
w,h
positive numbers giving width and height of the legend's box IN INCHES!
# the expression that produces the desired legend
expr <- ~graphics::legend(
# the top-left corner of the legend will appear in the
# top-right corner of the plot
x = essentials::fix.xlog(graphics::par("usr")[2L]),
y = essentials::fix.ylog(graphics::par("usr")[4L]),
legend = letters[1:5], fill = 1:5,
xpd = TRUE
)
# we'll start by drawing a plot with a legend without
# adjusting the margins. the margins will likely look too
# small or too large
graphics::plot(1:5) ; essentials::add.legend(expr)
# now, we'll adjust the margins using 'legend.dimensions'
# capture the dimensions of the resultant legend
ld <- essentials::legend.dimensions(expr)
essentials::adj.margins(ld)
graphics::plot(1:5) ; essentials::add.legend(expr)
# now, we'll adjust the legend such that it is centered
# vertically
essentials::location(expr) <- essentials::location(ld, adj = 0.5)
graphics::plot(1:5) ; essentials::add.legend(expr)
# you do not need to use 'legend.dimensions' explicitly, you
# could supply 'expr' directly to 'adj.margins' and
# 'location'
essentials::adj.margins(expr)
essentials::location(expr) <- essentials::location(expr, adj = 0.75)
graphics::plot(1:5) ; essentials::add.legend(expr)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.