Legend: Add legends to plots

LegendR Documentation

Add legends to plots

Description

This modified version of the base R function legend modifies the behaviour when plotting a legend on a point plot connected by lines: In this case, the base version adds the point character in the middle of the legend line, whereas this version adds the point characters to both ends of the legend line, when the parameter 'end.pch' is set to TRUE. The positioning of the end point charaters can be controlled by pch.xoff.

Usage

Legend(
  x,
  y = NULL,
  legend,
  fill = NULL,
  col = graphics::par("col"),
  border = "black",
  lty,
  lwd,
  pch,
  angle = 45,
  density = NULL,
  bty = "o",
  bg = graphics::par("bg"),
  box.lwd = graphics::par("lwd"),
  box.lty = graphics::par("lty"),
  box.col = graphics::par("fg"),
  pt.bg = NA,
  cex = 1,
  pt.cex = cex,
  pt.lwd = lwd,
  xjust = 0,
  yjust = 1,
  x.intersp = 1,
  y.intersp = 1,
  adj = c(0, 0.5),
  text.width = NULL,
  text.col = graphics::par("col"),
  text.font = NULL,
  merge = do.lines && has.pch,
  trace = FALSE,
  plot = TRUE,
  ncol = 1,
  horiz = FALSE,
  title = NULL,
  inset = 0,
  xpd,
  title.col = text.col[1],
  title.adj = 0.5,
  title.cex = cex[1],
  title.font = text.font[1],
  seg.len = 2,
  end.pch = FALSE,
  pch.xoff = 0.2
)

Arguments

x, y

the x and y co-ordinates to be used to position the legend. They can be specified by keyword or in any way which is accepted by xy.coords: See ‘Details’.

legend

a character or expression vector of length \ge 1 to appear in the legend. Other objects will be coerced by as.graphicsAnnot.

fill

if specified, this argument will cause boxes filled with the specified colors (or shaded in the specified colors) to appear beside the legend text.

col

the color of points or lines appearing in the legend.

border

the border color for the boxes (used only if fill is specified).

lty, lwd

the line types and widths for lines appearing in the legend. One of these two must be specified for line drawing.

pch

the plotting symbols appearing in the legend, as numeric vector or a vector of 1-character strings (see points). Unlike points, this can all be specified as a single multi-character string. Must be specified for symbol drawing.

angle

angle of shading lines.

density

the density of shading lines, if numeric and positive. If NULL or negative or NA color filling is assumed.

bty

the type of box to be drawn around the legend. The allowed values are "o" (the default) and "n".

bg

the background color for the legend box. (Note that this is only used if bty != "n".)

box.lty, box.lwd, box.col

the line type, width and color for the legend box (if bty = "o").

pt.bg

the background color for the points, corresponding to its argument bg.

cex

character expansion factor relative to current par("cex"). Used for text, and provides the default for pt.cex.

pt.cex

expansion factor(s) for the points.

pt.lwd

line width for the points, defaults to the one for lines, or if that is not set, to par("lwd").

xjust

how the legend is to be justified relative to the legend x location. A value of 0 means left justified, 0.5 means centered and 1 means right justified.

yjust

the same as xjust for the legend y location.

x.intersp

character interspacing factor for horizontal (x) spacing between symbol and legend text.

y.intersp

vertical (y) distances (in lines of text shared above/below each legend entry). A vector with one element for each row of the legend can be used.

adj

numeric of length 1 or 2; the string adjustment for legend text. Useful for y-adjustment when labels are plotmath expressions.

text.width

the width of the legend text in x ("user") coordinates. (Should be positive even for a reversed x axis.) Can be a single positive numeric value (same width for each column of the legend), a vector (one element for each column of the legend), NULL (default) for computing a proper maximum value of strwidth(legend)), or NA for computing a proper column wise maximum value of strwidth(legend)).

text.col

the color used for the legend text.

text.font

the font used for the legend text, see text.

merge

logical; if TRUE, merge points and lines but not filled boxes. Defaults to TRUE if there are points and lines.

trace

logical; if TRUE, shows how legend does all its magical computations.

plot

logical. If FALSE, nothing is plotted but the sizes are returned.

ncol

the number of columns in which to set the legend items (default is 1, a vertical legend).

horiz

logical; if TRUE, set the legend horizontally rather than vertically (specifying horiz overrides the ncol specification).

title

a character string or length-one expression giving a title to be placed at the top of the legend. Other objects will be coerced by as.graphicsAnnot.

inset

inset distance(s) from the margins as a fraction of the plot region when legend is placed by keyword.

xpd

if supplied, a value of the graphical parameter xpd to be used while the legend is being drawn.

title.col

color for title, defaults to text.col[1].

title.adj

horizontal adjustment for title: see the help for par("adj").

title.cex

expansion factor(s) for the title, defaults to cex[1].

title.font

the font used for the legend title, defaults to text.font[1], see text.

seg.len

the length of lines drawn to illustrate lty and/or lwd (in units of character widths).

end.pch

if TRUE, character symbols as specified by pch are added to the end of the legend lines specified by lty. Defaults to FALSE which reproduces the base behaviour.

pch.xoff

horizontal adjustment of the point characters at the end points of the legend lines to improve visual appearance; in units of character widths (defaults to 0.2).

Value

the value returned by legend.

Author(s)

R Core Team, Thomas Münch

Source

legend

See Also

legend

Examples


plot(x = 1 : 5, y = c(1, 5, 8, 3, 7), type = "b", pch = 19,
     xlab = "x", ylab = "y", ylim = c(0, 10))
legend("topleft", legend = "line with points from base `legend`", bty = "n",
       lty = 1, pch = 19)
Legend("topright", legend = "line with points from `Legend`", bty = "n",
       lty = 1, pch = 19, end.pch = TRUE, pch.xoff = 0.6, x.intersp = 1.5)

EarthSystemDiagnostics/grfxtools documentation built on Aug. 5, 2023, 1:43 p.m.