legend {base} | R Documentation |
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')
.
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, text.width = NULL, merge = FALSE)
x |
the x location of the legend. |
y |
the y location of the legend. |
legend |
a vector of text values or an |
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
|
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 |
x.intersp |
character interspacing factor for horizontal (x) spacing. |
y.intersp |
the same as |
text.width |
the width of the legend text in x ( |
merge |
logical; if |
plot
, barplot
which uses legend()
, and
text
for more examples of math expressions.
# 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])
##-- Math expressions:
plot (x, sin(x), type="l", col = 2, xlab = expression(phi))
lines(x, cos(x), col = 3, lty = 2); abline(h=0, v=pi/2*(-6:6), col="gray90")
ex.cs1 <- expression(plain(sin) * phi, plain(cos) * phi)
ex.cs2 <- expression(paste("sin ",phi), paste("cos ",phi))
legend(-3, .9, ex.cs1, lty=1:2, col=2:3)
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))
legend(4,30, ex12, col = 2:3, lty=2:3) # lwd = 2 does not yet work
rm(x, ex12, ex.cs1, ex.cs2)