[ top | up ]

Add Legends to Plots

Syntax

legend(x, y, legend, fill, col= "black",
       lty, pch, bty= "o", bg= par("bg"), cex= 1,
       xjust=0, yjust=1,  x.intersp= NULL, y.intersp= NULL, merge = FALSE)

Arguments

x the x location of the legend.
y the y location of the legend.
legend a vector of text values to appear in the legend.
fill if specied, this argument will cause boxes filled with the specified colors to appear beside the legend text.
col the color of points or lines appearing in the legend.
lty the line types for lines appearing in the legend.
pch the plotting symbols appearing in the legend.
bty the type of box to be drawn around the legend.
bg the background color for the legend box.
cex character expansion factor relative to current par("cex").
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.
y.intersp the same as x.intersp for vertical (y) line distances.
merge logical; if TRUE, "merge" points and lines but not filled boxes.

Description

This function can be used to add legends to plots. Note that a call to the function locator can be used in place of the x and y arguments.

The default values for x.intersp and y.intersp are computed from Cx <- cex * par('cex')

See Also

plot, barplot which uses legend(.).

Examples

##-- Run the example in '?matplot' or the following: matplot(c(1,8), c(0,4.5), type="n", xlab="Length", ylab="Width", main= "Petal and Sepal Dimensions in Iris Blossoms") leg.txt <- c(" Setosa Petals", " Setosa Sepals", "Versicolor Petals", "Versicolor Sepals") y.leg <- c( 4, 3, 2.2, 1.5, .9) cexv <- c( 1, .8, .6, .4, .25) for(i in seq(cexv)) legend(1, y.leg[i], leg.txt, pch="sSvV", col=c(1,3), cex = cexv[i]) ##-- '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=5) lines (x,tan(x), type='b', lty = 1, pch = 4, col=9) title("legend(..... lty= c(2,-1,1), pch= c(-1,3,4), merge = TRUE)",cex.main=1.1) vcex <- c( 3,1.5, 1, .8) vx <- c(-2, 0, 1.5, 2.8) for(i in seq(vcex)) legend(vx[i], 1.8, c("sin","cos","tan"), xjust = .5, col = c(3,5,9), lty= c(2,-1,1), pch= c(-1,3,4), merge=TRUE, cex = vcex[i])