legend.dimensions: Size of a Legend

legend.dimensionsR Documentation

Size of a Legend

Description

This function is used to determine the size of a legend as it appears on a plot.

Usage

legend.dimensions(expr, envir = parent.frame(),
                        enclos = if (is.list(envir) || is.pairlist(envir))
                                     parent.frame() else baseenv(),
    trace = FALSE)

Arguments

expr

a call, an expression of length 1, or a formula of length 2.

envir

the environment in which expr is to be evaluated. May also be NULL, a list, a data frame, a pairlist or an integer as specified to sys.call.

enclos

Relevant when envir is a (pair)list or a data frame. Specifies the enclosure, i.e., where R looks for objects not found in envir. This can be NULL (interpreted as the base package environment, baseenv()) or an environment.

trace

Should legend.dimensions print debugging messages?

Details

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.

Value

A list with components:

w,h

positive numbers giving width and height of the legend's box IN INCHES!

Examples


# 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)

ArcadeAntics/essentials documentation built on Nov. 7, 2024, 4:33 p.m.