legend function, accept it has an extra parameter, seg.len which allows the user to change the lengths of lines shown in legends.
legend2(x, y = NULL, legend, fill = NULL, col = par("col"), lty, lwd, pch, angle = 45, density = NULL, bty = "o", bg = par("bg"), box.lwd = par("lwd"), box.lty = par("lty"), 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"), merge = do.lines && has.pch, trace = FALSE, plot = TRUE, ncol = 1, horiz = FALSE, title = NULL, inset = 0, seg.len = 2)xy.coords: See Details.as.graphicsAnnot.NULL or negative or NA color filling
is assumed."o" (the default) and "n".bty != "n".)points,
corresponding to its argument bg.par("cex").par("lwd").xjust for the legend y location.labels are
plotmath expressions."user")
coordinates. (Should be positive even for a reversed x axis.)
Defaults to the proper value computed by strwidth(legend).TRUE, merge points and lines but
not filled boxes. Defaults to TRUE if there are points and lines.TRUE, shows how legend does all
its magical computations.FALSE, nothing is plotted but the
sizes are returned.TRUE, set the legend horizontally
rather than vertically (specifying horiz overrides the ncol
specification).as.graphicsAnnot.w, hleft, topx, ylength(legend),
giving the x and y coordinates of the legend's text(s).x, y, legend are interpreted in a non-standard way to
allow the coordinates to be specified via one or two arguments.
If legend is missing and y is not numeric, it is assumed
that the second argument is intended to be legend and that the
first argument specifies the coordinates. The coordinates can be specified in any way which is accepted by
xy.coords. 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).
The location may also be specified by setting x to a single
keyword from the list "bottomright", "bottom",
"bottomleft", "left", "topleft",
"top", "topright", "right" and
"center". This places the legend on the inside of the plot
frame at the given location. Partial argument matching is used. The
optional inset 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 x-
distance, the second for y-distance.
Attribute arguments such as col, pch, lty,
etc, are recycled if necessary. merge is not.
Points are drawn after lines in order that they can cover the
line with their background color pt.bg, if applicable.
See the examples for how to right-justify labels.
Murrell, P. (2005) R Graphics. Chapman & Hall/CRC Press.
legend
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(-1,3,4), merge = TRUE)",
cex.main = 1.1)
legend2(-1, 1.9, c("sin", "cos", "tan"), col = c(3,4,6),
text.col = "green4", lty = c(2, -1, 1), pch = c(-1, 3, 4),
merge = TRUE, bg = 'gray90', seg.len=6)
Run the code above in your browser using DataLab