legend
位于 graphics
包(package)。 说明
此函数可用于向绘图添加图例。请注意,可以使用对函数locator(1)
的调用来代替x
和y
参数。
用法
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[1], title.adj = 0.5,
title.cex = cex[1], title.font = text.font[1],
seg.len = 2)
参数
x, y |
用于定位图例的 x 和 y 坐标。它们可以通过关键字或 |
legend |
图例中出现的字符或长度为 expression 向量。其他对象将被 |
fill |
如果指定,此参数将导致填充指定颜色(或以指定颜色着色)的框出现在图例文本旁边。 |
col |
图例中出现的点或线的颜色。 |
border |
框的边框颜色(仅在指定 |
lty, lwd |
图例中出现的线条类型和宽度。必须指定这两者之一来绘制线条。 |
pch |
图例中出现的绘图符号,作为数值向量或 1 字符字符串向量(请参阅 |
angle |
阴影线的角度。 |
density |
阴影线的密度(如果为数字且为正值)。如果 |
bty |
要在图例周围绘制的框的类型。允许的值为 |
bg |
图例框的背景颜色。 (请注意,这仅在 |
box.lty, box.lwd, box.col |
图例框的线条类型、宽度和颜色(如果 |
pt.bg |
|
cex |
相对于当前 |
pt.cex |
点的扩展因子。 |
pt.lwd |
点的线宽,默认为线的线宽,或者如果未设置,则为 |
xjust |
如何相对于图例 x 位置调整图例。值 0 表示左对齐,0.5 表示居中,1 表示右对齐。 |
yjust |
与图例 y 位置的 |
x.intersp |
符号和图例文本之间的水平 (x) 间距的字符间距因子。 |
y.intersp |
垂直 (y) 距离(以每个图例条目上方/下方共享的文本行为单位)。可以使用图例的每一行具有一个元素的向量。 |
adj |
长度为 1 或 2 的数字;图例文本的字符串调整。当 |
text.width |
x ( |
text.col |
用于图例文本的颜色。 |
text.font |
用于图例文本的字体,请参阅 |
merge |
逻辑性;如果 |
trace |
逻辑性; if |
plot |
合乎逻辑的。如果 |
ncol |
设置图例项的列数(默认为 1,垂直图例)。 |
horiz |
逻辑性;如果 |
title |
字符串或长度为 1 的表达式,给出要放置在图例顶部的标题。其他对象将被 |
inset |
当通过关键字放置图例时,距边距的插入距离作为绘图区域的一部分。 |
xpd |
如果提供,则绘制图例时要使用的 graphical parameter |
title.col |
|
title.adj |
|
title.cex |
标题的扩展因子,默认为 |
title.font |
用于图例标题的字体,默认为 |
seg.len |
用于说明 |
细节
参数 x
、 y
、 legend
以非标准方式解释,以允许通过一个或两个参数指定坐标。如果 legend
缺失且 y
不是数字,则假定第二个参数为 legend
,并且第一个参数指定坐标。
坐标可以以 xy.coords
接受的任何方式指定。如果给出一个点的坐标,则将其用作包含图例的矩形的左上角坐标。如果它给出两个点的坐标,则这些点指定矩形的对角(任意一对角,顺序任意)。
还可以通过将x
设置为列表"bottomright"
, "bottom"
, "bottomleft"
, "left"
, "topleft"
, "top"
, "topright"
, "right"
和"center"
中的单个关键字来指定该位置。这会将图例放置在图框内部的给定位置。使用部分参数匹配。可选的 inset
参数指定图例距绘图边距的插入距离。如果给出单个值,则该值将用于两个边距;如果给出两个值,第一个值用于 x
- 距离,第二个值用于 y
- 距离。
如有必要,属性参数(例如 col
、 pch
、 lty
等)将被回收:merge
则不会。将lty
条目设置为0
或将lwd
条目设置为NA
以抑制相应图例条目中的行;将 pch
值设置为 NA
以抑制点。
点在线条之后绘制,以便它们可以用背景颜色 pt.bg
覆盖线条(如果适用)。
请参阅示例以了解如何右对齐标签。
由于它们不用于 Unicode 代码点,因此 -31:-1
值将被默默省略,NA
和 ""
值也是如此。
值
包含列表组件的列表
rect |
包含组件的列表
|
text |
包含组件的列表
|
无形中返回了。
例子
## 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("right", leg.txt, pch = "sSvV", col = c(1, 3),
cex = 1+(-1:2)/8, trace = TRUE)# trace: show computed lengths & coords
## 'merge = TRUE' for merging lines & points:
x <- seq(-pi, pi, length.out = 65)
for(reverse in c(FALSE, TRUE)) { ## normal *and* reverse axes:
F <- if(reverse) rev else identity
plot(x, sin(x), type = "l", col = 3, lty = 2,
xlim = F(range(x)), ylim = F(c(-1.2, 1.8)))
points(x, cos(x), pch = 3, col = 4)
lines(x, tan(x), type = "b", lty = 1, pch = 4, col = 6)
title("legend('top', lty = c(2, -1, 1), pch = c(NA, 3, 4), merge = TRUE)",
cex.main = 1.1)
legend("top", 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", trace=TRUE)
} # for(..)
## 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, inset = 1/10,
title = "Line Types", title.cex = 0.5, trace=TRUE)
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) #, trace = TRUE)
} # ^^^^^^^ to force lines -> automatic merge=TRUE
par(mfrow = c(1,1))
##-- Math expressions: ------------------------------
x <- seq(-pi, pi, length.out = 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 -- see also example(barplot) which may call legend(*, fill=)
barplot(VADeaths)
legend("topright", rownames(VADeaths), fill = gray.colors(nrow(VADeaths)))
## Using 'ncol'
x <- 0:64/64
for(R in c(identity, rev)) { # normal *and* reverse x-axis works fine:
xl <- R(range(x)); x1 <- xl[1]
matplot(x, outer(x, 1:7, function(x, k) sin(k * pi * x)), xlim=xl,
type = "o", col = 1:7, ylim = c(-1, 1.5), pch = "*")
op <- par(bg = "antiquewhite1")
legend(x1, 1.5, paste("sin(", 1:7, "pi * x)"), col = 1:7, lty = 1:7,
pch = "*", ncol = 4, cex = 0.8)
legend("bottomright", paste("sin(", 1:7, "pi * x)"), col = 1:7, lty = 1:7,
pch = "*", cex = 0.8)
legend(x1, -.1, paste("sin(", 1:4, "pi * x)"), col = 1:4, lty = 1:4,
ncol = 2, cex = 0.8)
legend(x1, -.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)
} # for(..)
## 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)
# using text.width for several columns
plot(1, type="n")
legend("topleft", c("This legend", "has", "equally sized", "columns."),
pch = 1:4, ncol = 4)
legend("bottomleft", c("This legend", "has", "optimally sized", "columns."),
pch = 1:4, ncol = 4, text.width = NA)
legend("right", letters[1:4], pch = 1:4, ncol = 4,
text.width = 1:4 / 50)
参考
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) The New S Language. Wadsworth & Brooks/Cole.
Murrell, P. (2005) R Graphics. Chapman & Hall/CRC Press.
也可以看看
相关用法
- R layout 指定复杂的绘图安排
- R locator 图形输入
- R lines 将连接的线段添加到绘图中
- R barplot 条形图
- R plot.histogram 绘制直方图
- R points 向绘图添加点
- R stem 茎叶图
- R mtext 将文本写入绘图的边距
- R arrows 将箭头添加到绘图中
- R contour 显示轮廓
- R pairs 散点图矩阵
- R stars 星图(蜘蛛图/雷达图)和线段图
- R box 在地块周围画一个方框
- R coplot 调节图
- R smoothScatter 具有平滑密度颜色表示的散点图
- R mosaicplot 马赛克图
- R bxp 从摘要中绘制箱线图
- R plot.raster 绘制光栅图像
- R axTicks 计算轴刻度线位置
- R curve 绘制函数图
- R plot.factor 绘制因子变量
- R sunflowerplot 制作向日葵散点图
- R plot.table 表对象的绘图方法
- R units 图形单位
- R identify 识别散点图中的点
注:本文由纯净天空筛选整理自R-devel大神的英文原创作品 Add Legends to Plots。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。