EVOLUTION-MANAGER
Edit File: legend.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>R: Add Legends to Plots</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link rel="stylesheet" type="text/css" href="R.css" /> </head><body> <table width="100%" summary="page for legend {graphics}"><tr><td>legend {graphics}</td><td style="text-align: right;">R Documentation</td></tr></table> <h2>Add Legends to Plots</h2> <h3>Description</h3> <p>This function can be used to add legends to plots. Note that a call to the function <code><a href="locator.html">locator</a>(1)</code> can be used in place of the <code>x</code> and <code>y</code> arguments. </p> <h3>Usage</h3> <pre> legend(x, y = NULL, legend, fill = NULL, col = par("col"), border = "black", lty, lwd, pch, angle = 45, density = NULL, bty = "o", bg = par("bg"), box.lwd = par("lwd"), box.lty = par("lty"), box.col = 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 = 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, title.adj = 0.5, seg.len = 2) </pre> <h3>Arguments</h3> <table summary="R argblock"> <tr valign="top"><td><code>x, y</code></td> <td> <p>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 <code><a href="../../grDevices/html/xy.coords.html">xy.coords</a></code>: See ‘Details’.</p> </td></tr> <tr valign="top"><td><code>legend</code></td> <td> <p>a character or <a href="../../base/html/expression.html">expression</a> vector of length <i>≥ 1</i> to appear in the legend. Other objects will be coerced by <code><a href="../../grDevices/html/as.graphicsAnnot.html">as.graphicsAnnot</a></code>.</p> </td></tr> <tr valign="top"><td><code>fill</code></td> <td> <p>if specified, this argument will cause boxes filled with the specified colors (or shaded in the specified colors) to appear beside the legend text.</p> </td></tr> <tr valign="top"><td><code>col</code></td> <td> <p>the color of points or lines appearing in the legend.</p> </td></tr> <tr valign="top"><td><code>border</code></td> <td> <p>the border color for the boxes (used only if <code>fill</code> is specified).</p> </td></tr> <tr valign="top"><td><code>lty, lwd</code></td> <td> <p>the line types and widths for lines appearing in the legend. One of these two <em>must</em> be specified for line drawing.</p> </td></tr> <tr valign="top"><td><code>pch</code></td> <td> <p>the plotting symbols appearing in the legend, as numeric vector or a vector of 1-character strings (see <code><a href="points.html">points</a></code>). Unlike <code>points</code>, this can all be specified as a single multi-character string. <em>Must</em> be specified for symbol drawing.</p> </td></tr> <tr valign="top"><td><code>angle</code></td> <td> <p>angle of shading lines.</p> </td></tr> <tr valign="top"><td><code>density</code></td> <td> <p>the density of shading lines, if numeric and positive. If <code>NULL</code> or negative or <code>NA</code> color filling is assumed.</p> </td></tr> <tr valign="top"><td><code>bty</code></td> <td> <p>the type of box to be drawn around the legend. The allowed values are <code>"o"</code> (the default) and <code>"n"</code>.</p> </td></tr> <tr valign="top"><td><code>bg</code></td> <td> <p>the background color for the legend box. (Note that this is only used if <code>bty != "n"</code>.)</p> </td></tr> <tr valign="top"><td><code>box.lty, box.lwd, box.col</code></td> <td> <p>the line type, width and color for the legend box (if <code>bty = "o"</code>).</p> </td></tr> <tr valign="top"><td><code>pt.bg</code></td> <td> <p>the background color for the <code><a href="points.html">points</a></code>, corresponding to its argument <code>bg</code>.</p> </td></tr> <tr valign="top"><td><code>cex</code></td> <td> <p>character expansion factor <b>relative</b> to current <code>par("cex")</code>. Used for text, and provides the default for <code>pt.cex</code>.</p> </td></tr> <tr valign="top"><td><code>pt.cex</code></td> <td> <p>expansion factor(s) for the points.</p> </td></tr> <tr valign="top"><td><code>pt.lwd</code></td> <td> <p>line width for the points, defaults to the one for lines, or if that is not set, to <code>par("lwd")</code>.</p> </td></tr> <tr valign="top"><td><code>xjust</code></td> <td> <p>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.</p> </td></tr> <tr valign="top"><td><code>yjust</code></td> <td> <p>the same as <code>xjust</code> for the legend y location.</p> </td></tr> <tr valign="top"><td><code>x.intersp</code></td> <td> <p>character interspacing factor for horizontal (x) spacing.</p> </td></tr> <tr valign="top"><td><code>y.intersp</code></td> <td> <p>the same for vertical (y) line distances.</p> </td></tr> <tr valign="top"><td><code>adj</code></td> <td> <p>numeric of length 1 or 2; the string adjustment for legend text. Useful for y-adjustment when <code>labels</code> are <a href="../../grDevices/html/plotmath.html">plotmath</a> expressions.</p> </td></tr> <tr valign="top"><td><code>text.width</code></td> <td> <p>the width of the legend text in x (<code>"user"</code>) coordinates. (Should be a single positive number even for a reversed x axis.) Defaults to the proper value computed by <code><a href="strwidth.html">strwidth</a>(legend)</code>.</p> </td></tr> <tr valign="top"><td><code>text.col</code></td> <td> <p>the color used for the legend text.</p> </td></tr> <tr valign="top"><td><code>text.font</code></td> <td> <p>the font used for the legend text, see <code><a href="text.html">text</a></code>.</p> </td></tr> <tr valign="top"><td><code>merge</code></td> <td> <p>logical; if <code>TRUE</code>, merge points and lines but not filled boxes. Defaults to <code>TRUE</code> if there are points and lines.</p> </td></tr> <tr valign="top"><td><code>trace</code></td> <td> <p>logical; if <code>TRUE</code>, shows how <code>legend</code> does all its magical computations.</p> </td></tr> <tr valign="top"><td><code>plot</code></td> <td> <p>logical. If <code>FALSE</code>, nothing is plotted but the sizes are returned.</p> </td></tr> <tr valign="top"><td><code>ncol</code></td> <td> <p>the number of columns in which to set the legend items (default is 1, a vertical legend).</p> </td></tr> <tr valign="top"><td><code>horiz</code></td> <td> <p>logical; if <code>TRUE</code>, set the legend horizontally rather than vertically (specifying <code>horiz</code> overrides the <code>ncol</code> specification).</p> </td></tr> <tr valign="top"><td><code>title</code></td> <td> <p>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 <code><a href="../../grDevices/html/as.graphicsAnnot.html">as.graphicsAnnot</a></code>.</p> </td></tr> <tr valign="top"><td><code>inset</code></td> <td> <p>inset distance(s) from the margins as a fraction of the plot region when legend is placed by keyword.</p> </td></tr> <tr valign="top"><td><code>xpd</code></td> <td> <p>if supplied, a value of the <a href="par.html">graphical parameter</a> <code>xpd</code> to be used while the legend is being drawn.</p> </td></tr> <tr valign="top"><td><code>title.col</code></td> <td> <p>color for <code>title</code>.</p> </td></tr> <tr valign="top"><td><code>title.adj</code></td> <td> <p>horizontal adjustment for <code>title</code>: see the help for <code><a href="par.html">par</a>("adj")</code>.</p> </td></tr> <tr valign="top"><td><code>seg.len</code></td> <td> <p>the length of lines drawn to illustrate <code>lty</code> and/or <code>lwd</code> (in units of character widths).</p> </td></tr> </table> <h3>Details</h3> <p>Arguments <code>x</code>, <code>y</code>, <code>legend</code> are interpreted in a non-standard way to allow the coordinates to be specified <em>via</em> one or two arguments. If <code>legend</code> is missing and <code>y</code> is not numeric, it is assumed that the second argument is intended to be <code>legend</code> and that the first argument specifies the coordinates. </p> <p>The coordinates can be specified in any way which is accepted by <code><a href="../../grDevices/html/xy.coords.html">xy.coords</a></code>. If this gives the coordinates of one point, it is used as the top-left coordinate of the rectangle containing the legend. If it gives the coordinates of two points, these specify opposite corners of the rectangle (either pair of corners, in any order). </p> <p>The location may also be specified by setting <code>x</code> to a single keyword from the list <code>"bottomright"</code>, <code>"bottom"</code>, <code>"bottomleft"</code>, <code>"left"</code>, <code>"topleft"</code>, <code>"top"</code>, <code>"topright"</code>, <code>"right"</code> and <code>"center"</code>. This places the legend on the inside of the plot frame at the given location. Partial argument matching is used. The optional <code>inset</code> argument specifies how far the legend is inset from the plot margins. If a single value is given, it is used for both margins; if two values are given, the first is used for <code>x</code>- distance, the second for <code>y</code>-distance. </p> <p>Attribute arguments such as <code>col</code>, <code>pch</code>, <code>lty</code>, etc, are recycled if necessary: <code>merge</code> is not. Set entries of <code>lty</code> to <code>0</code> or set entries of <code>lwd</code> to <code>NA</code> to suppress lines in corresponding legend entries; set <code>pch</code> values to <code>NA</code> to suppress points. </p> <p>Points are drawn <em>after</em> lines in order that they can cover the line with their background color <code>pt.bg</code>, if applicable. </p> <p>See the examples for how to right-justify labels. </p> <p>Since they are not used for Unicode code points, values <code>-31:-1</code> are silently omitted, as are <code>NA</code> and <code>""</code> values. </p> <h3>Value</h3> <p>A list with list components </p> <table summary="R valueblock"> <tr valign="top"><td><code>rect</code></td> <td> <p>a list with components </p> <dl> <dt><code>w</code>, <code>h</code></dt><dd><p>positive numbers giving <b>w</b>idth and <b>h</b>eight of the legend's box.</p> </dd> <dt><code>left</code>, <code>top</code></dt><dd><p>x and y coordinates of upper left corner of the box.</p> </dd> </dl> </td></tr> <tr valign="top"><td><code>text</code></td> <td> <p>a list with components </p> <dl> <dt><code>x, y</code></dt><dd><p>numeric vectors of length <code>length(legend)</code>, giving the x and y coordinates of the legend's text(s).</p> </dd> </dl> </td></tr> </table> <p>returned invisibly. </p> <h3>References</h3> <p>Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) <em>The New S Language</em>. Wadsworth & Brooks/Cole. </p> <p>Murrell, P. (2005) <em>R Graphics</em>. Chapman & Hall/CRC Press. </p> <h3>See Also</h3> <p><code><a href="plot.html">plot</a></code>, <code><a href="barplot.html">barplot</a></code> which uses <code>legend()</code>, and <code><a href="text.html">text</a></code> for more examples of math expressions. </p> <h3>Examples</h3> <pre> ## Run the example in '?matplot' or the following: leg.txt <- c("Setosa Petals", "Setosa Sepals", "Versicolor Petals", "Versicolor Sepals") y.leg <- c(4.5, 3, 2.1, 1.4, .7) cexv <- c(1.2, 1, 4/5, 2/3, 1/2) matplot(c(1, 8), c(0, 4.5), type = "n", xlab = "Length", ylab = "Width", main = "Petal and Sepal Dimensions in Iris Blossoms") for (i in seq(cexv)) { text (1, y.leg[i] - 0.1, paste("cex=", formatC(cexv[i])), cex = 0.8, adj = 0) legend(3, y.leg[i], leg.txt, pch = "sSvV", col = c(1, 3), cex = cexv[i]) } ## cex *vector* [in R <= 3.5.1 has 'if(xc < 0)' w/ length(xc) == 2] legend(6,1, leg.txt, pch = "sSvV", col = c(1, 3), cex = 1+(-1:2)/8) ## 'merge = TRUE' for merging lines & points: x <- seq(-pi, pi, len = 65) plot(x, sin(x), type = "l", ylim = c(-1.2, 1.8), col = 3, lty = 2) points(x, cos(x), pch = 3, col = 4) lines(x, tan(x), type = "b", lty = 1, pch = 4, col = 6) title("legend(..., lty = c(2, -1, 1), pch = c(NA, 3, 4), merge = TRUE)", cex.main = 1.1) legend(-1, 1.9, c("sin", "cos", "tan"), col = c(3, 4, 6), text.col = "green4", lty = c(2, -1, 1), pch = c(NA, 3, 4), merge = TRUE, bg = "gray90") ## right-justifying a set of labels: thanks to Uwe Ligges x <- 1:5; y1 <- 1/x; y2 <- 2/x plot(rep(x, 2), c(y1, y2), type = "n", xlab = "x", ylab = "y") lines(x, y1); lines(x, y2, lty = 2) temp <- legend("topright", legend = c(" ", " "), text.width = strwidth("1,000,000"), lty = 1:2, xjust = 1, yjust = 1, title = "Line Types") text(temp$rect$left + temp$rect$w, temp$text$y, c("1,000", "1,000,000"), pos = 2) ##--- log scaled Examples ------------------------------ leg.txt <- c("a one", "a two") par(mfrow = c(2, 2)) for(ll in c("","x","y","xy")) { plot(2:10, log = ll, main = paste0("log = '", ll, "'")) abline(1, 1) lines(2:3, 3:4, col = 2) points(2, 2, col = 3) rect(2, 3, 3, 2, col = 4) text(c(3,3), 2:3, c("rect(2,3,3,2, col=4)", "text(c(3,3),2:3,\"c(rect(...)\")"), adj = c(0, 0.3)) legend(list(x = 2,y = 8), legend = leg.txt, col = 2:3, pch = 1:2, lty = 1, merge = TRUE) #, trace = TRUE) } par(mfrow = c(1,1)) ##-- Math expressions: ------------------------------ x <- seq(-pi, pi, len = 65) plot(x, sin(x), type = "l", col = 2, xlab = expression(phi), ylab = expression(f(phi))) abline(h = -1:1, v = pi/2*(-6:6), col = "gray90") lines(x, cos(x), col = 3, lty = 2) ex.cs1 <- expression(plain(sin) * phi, paste("cos", phi)) # 2 ways utils::str(legend(-3, .9, ex.cs1, lty = 1:2, plot = FALSE, adj = c(0, 0.6))) # adj y ! legend(-3, 0.9, ex.cs1, lty = 1:2, col = 2:3, adj = c(0, 0.6)) require(stats) x <- rexp(100, rate = .5) hist(x, main = "Mean and Median of a Skewed Distribution") abline(v = mean(x), col = 2, lty = 2, lwd = 2) abline(v = median(x), col = 3, lty = 3, lwd = 2) ex12 <- expression(bar(x) == sum(over(x[i], n), i == 1, n), hat(x) == median(x[i], i == 1, n)) utils::str(legend(4.1, 30, ex12, col = 2:3, lty = 2:3, lwd = 2)) ## 'Filled' boxes -- for more, see example(plot.factor) op <- par(bg = "white") # to get an opaque box for the legend plot(cut(weight, 3) ~ group, data = PlantGrowth, col = NULL, density = 16*(1:3)) par(op) ## Using 'ncol' : x <- 0:64/64 matplot(x, outer(x, 1:7, function(x, k) sin(k * pi * x)), type = "o", col = 1:7, ylim = c(-1, 1.5), pch = "*") op <- par(bg = "antiquewhite1") legend(0, 1.5, paste("sin(", 1:7, "pi * x)"), col = 1:7, lty = 1:7, pch = "*", ncol = 4, cex = 0.8) legend(.8,1.2, paste("sin(", 1:7, "pi * x)"), col = 1:7, lty = 1:7, pch = "*", cex = 0.8) legend(0, -.1, paste("sin(", 1:4, "pi * x)"), col = 1:4, lty = 1:4, ncol = 2, cex = 0.8) legend(0, -.4, paste("sin(", 5:7, "pi * x)"), col = 4:6, pch = 24, ncol = 2, cex = 1.5, lwd = 2, pt.bg = "pink", pt.cex = 1:3) par(op) ## point covering line : y <- sin(3*pi*x) plot(x, y, type = "l", col = "blue", main = "points with bg & legend(*, pt.bg)") points(x, y, pch = 21, bg = "white") legend(.4,1, "sin(c x)", pch = 21, pt.bg = "white", lty = 1, col = "blue") ## legends with titles at different locations plot(x, y, type = "n") legend("bottomright", "(x,y)", pch=1, title= "bottomright") legend("bottom", "(x,y)", pch=1, title= "bottom") legend("bottomleft", "(x,y)", pch=1, title= "bottomleft") legend("left", "(x,y)", pch=1, title= "left") legend("topleft", "(x,y)", pch=1, title= "topleft, inset = .05", inset = .05) legend("top", "(x,y)", pch=1, title= "top") legend("topright", "(x,y)", pch=1, title= "topright, inset = .02",inset = .02) legend("right", "(x,y)", pch=1, title= "right") legend("center", "(x,y)", pch=1, title= "center") # using text.font (and text.col): op <- par(mfrow = c(2, 2), mar = rep(2.1, 4)) c6 <- terrain.colors(10)[1:6] for(i in 1:4) { plot(1, type = "n", axes = FALSE, ann = FALSE); title(paste("text.font =",i)) legend("top", legend = LETTERS[1:6], col = c6, ncol = 2, cex = 2, lwd = 3, text.font = i, text.col = c6) } par(op) </pre> <hr /><div style="text-align: center;">[Package <em>graphics</em> version 3.6.0 <a href="00Index.html">Index</a>]</div> </body></html>