找回密码
 注册
查看: 15413|回复: 0

R语言:legend()函数中文帮助文档(中英文对照)

[复制链接]
发表于 2012-2-16 17:36:46 | 显示全部楼层 |阅读模式
legend(graphics)
legend()所属R语言包:graphics

                                        Add Legends to Plots
                                         加入到图的传奇

                                         译者:生物统计家园网 机器人LoveR

描述----------Description----------

This function can be used to add legends to plots.  Note that a call to the function locator(1) can be used in place of the x and y arguments.
此功能可用于添加到图的传说。请注意,呼叫的功能locator(1)可以在x和y参数的地方使用。


用法----------Usage----------


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"),
       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)



参数----------Arguments----------

参数:x, y
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 xy.coords: See "Details".
X和Y坐标可用于定位的传说。他们可以指定关键字或在任何xy.coords接受的方式:请参阅“详细资料”。


参数:legend
a character or expression vector. of length ≥ 1 to appear in the legend.  Other objects will be coerced by as.graphicsAnnot.
字符或表达向量。长度≥ 1出现传说中的。其他对象将被裹挟as.graphicsAnnot。


参数:fill
if specified, this argument will cause boxes filled with the specified colors (or shaded in the specified colors) to appear beside the legend text.
如果指定的话,这种说法会导致填充指定的颜色(或在指定的颜色阴影)的出现旁边的图例文本框。


参数:col
the color of points or lines appearing in the legend.
出现在传说中的点或线的颜色。


参数:border
the border color for the boxes (used only if fill is specified).
框的边框颜色(仅用于如果fill指定)。


参数:lty, lwd
the line types and widths for lines appearing in the legend.  One of these two must be specified for line drawing.
在传说中出现的行线类型和宽度。这两个必须指定画线。


参数:pch
the plotting symbols appearing in the legend, either as vector of 1-character strings, or one (multi character) string.  Must be specified for symbol drawing.
绘图符号出现在传说中,无论是作为向量1个字符的字符串,或一个(多字符)字符串。必须指定图形符号。


参数:angle
angle of shading lines.
角底纹线条。


参数:density
the density of shading lines, if numeric and positive. If NULL or negative or NA color filling is assumed.
阴影线的密度,如果数字和正面的。如果NULL或负或假设NA颜色填充。


参数:bty
the type of box to be drawn around the legend.  The allowed values are "o" (the default) and "n".
框中键入要绘制各地的传说。允许的值是"o"(默认)和"n"。


参数:bg
the background color for the legend box.  (Note that this is only used if bty != "n".)
传说中的背景颜色。 (请注意,这是仅用于如果bty != "n"。)


参数:box.lty, box.lwd, box.col
the line type, width and color for the legend box (if bty = "o").
如果bty = "o"传说中的线路类型,宽度和颜色()。


参数:pt.bg
the background color for the points, corresponding to its argument bg.
的points,相应参数bg的背景颜色。


参数:cex
character expansion factor relative to current par("cex").  Used for text, and provides the default for pt.cex and title.cex.
字符的膨胀系数相对于当前par("cex")。用于文本,并提供了默认的pt.cex和title.cex。


参数:pt.cex
expansion factor(s) for the points.
膨胀系数(S)点。


参数:pt.lwd
line width for the points, defaults to the one for lines, or if that is not set, to par("lwd").
线宽点,默认为一条线路,或如果没有设置,par("lwd")。


参数: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.
传说是传说中的x位置相对合理。值为0表示左对齐,0.5表示居中,右对齐,1表示。


参数:yjust
the same as xjust for the legend y location.
xjust传说y位置相同。


参数:x.intersp
character interspacing factor for horizontal (x) spacing.
水平(X)的间距字符间距的因素。


参数:y.intersp
the same for vertical (y) line distances.
垂直(y)线距离相同。


参数:adj
numeric of length 1 or 2; the string adjustment for legend text.  Useful for y-adjustment when labels are plotmath expressions.
调整图例文本字符串长度为1或2个数字。 Y-调整时,labels是plotmath的表达有用。


参数:text.width
the width of the legend text in x ("user") coordinates.  (Should be positive even for a reversed x axis.) Defaults to the proper value computed by strwidth(legend).
X("user")坐标的传说文本的宽度。 (应该是积极甚至为扭转的X轴。)默认为strwidth(legend)计算正确的值。


参数:text.col
the color used for the legend text.
用于图例文本的颜色。


参数:merge
logical; if TRUE, merge points and lines but not filled boxes.  Defaults to TRUE if there are points and lines.
逻辑;如果TRUE,合并点,线,但没有填写框。默认为TRUE如果有是点和线。


参数:trace
logical; if TRUE, shows how legend does all its magical computations.
逻辑;如果TRUE,显示了如何legend这一切神奇的计算。


参数:plot
logical.  If FALSE, nothing is plotted but the sizes are returned.
逻辑。如果FALSE,没有绘制,但大小返回。


参数:ncol
the number of columns in which to set the legend items (default is 1, a vertical legend).
数列在其中设置图例项(默认为1,一个垂直的传说)。


参数:horiz
logical; if TRUE, set the legend horizontally rather than vertically (specifying horiz overrides the ncol specification).
逻辑;如果TRUE,设置水平的传说,而不是垂直(指定horiz覆盖ncol规范)。


参数:title
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 as.graphicsAnnot.
字符串或长度一表达,给人一种被放置在传说顶部的标题。其他对象将被裹挟as.graphicsAnnot。


参数:inset
inset distance(s) from the margins as a fraction of the plot region when legend is placed by keyword.
的插图距离(S)时,传说是由关键字放置的图区域的一小部分利润。


参数:xpd
if supplied, a value of the graphical parameter xpd to be used while the legend is being drawn.
如果提供,图形参数值xpd被使用,而正在制定的传说。


参数:title.col
color for title.
颜色title。


参数:title.adj
horizontal adjustment for title: see the help for par("adj").
水平调整title:看到par("adj")的帮助。


参数:seg.len
the length of lines drawn to illustrate lty and/or lwd (in units of character widths).
说明lty和/或lwd(字符宽度为单位)制定的行的长度。


Details

详情----------Details----------

Arguments 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.
参数x,y,legend解释在一个非标准的方式允许通过一个或两个参数指定的坐标。如果legend丢失y是不是数字,假设第二个参数的目的是legend“,第一个参数指定的坐标。

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).
坐标,可以指定在任何xy.coords接受的方式。如果这给出了一个点的坐标,它被用作包含传说中的矩形的左上角坐标。如果它使两个点的坐标,这些指定的矩形的对角(无论任何顺序对弯道)。

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.
也可以通过设置x从单一关键字列表指定位置"bottomright","bottom","bottomleft","left","topleft" "top","topright","right"和"center"。这对传说的图框架内,在指定位置。部分参数匹配使用。可选inset参数指定多远,传说是从图利润率插图。一个单一的值,如果它被用于两个边缘,如果这两个值,第一个是用于x - 距离,第二y距离。

Attribute arguments such as col, pch, lty, etc, are recycled if necessary: merge is not.
属性参数,如col,pch,lty“等,被回收,如果有必要:merge是不是。

Points are drawn after lines in order that they can cover the line with their background color pt.bg, if applicable.
点绘制订单行后,他们可以覆盖行的背景颜色pt.bg,如适用。

See the examples for how to right-justify labels.
如何右对齐标签的例子。


值----------Value----------

A list with list components
列表组件的列表


参数:rect
a list with components     
一个组件的列表

w, hpositive numbers giving width and height of the legend's box.  
w,h给传说中的盒子的宽度和高度的正数。

left, topx and y coordinates of upper left corner of the box.     
left,topx和y坐标上框的左上角。


参数:text
a list with components     
一个组件的列表

x, ynumeric vectors of length length(legend), giving the x and y coordinates of the legend's text(s).     
x, y长度的数值向量length(legend),使传说中的文本(S)x和y坐标。

returned invisibly.
返回无形。


参考文献----------References----------

The New S Language. Wadsworth & Brooks/Cole.


参见----------See Also----------

plot, barplot which uses legend(), and text for more examples of math expressions.
plot,barplot使用legend()和text更多的数学表达式的例子。


举例----------Examples----------


## Run the example in '?matplot' or the following:[#运行matplot?“或以下的例子:]
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]-.1, paste("cex=",formatC(cexv[i])), cex=.8, adj = 0)
  legend(3, y.leg[i], leg.txt, pch = "sSvV", col = c(1, 3), cex = cexv[i])
}

## 'merge = TRUE' for merging lines &amp; points:[#合并合并线和点= TRUE“:]
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)
legend(-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')

## right-justifying a set of labels: thanks to Uwe Ligges[#右对齐的标签集:感谢乌韦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=paste("log = '",ll,"'", sep=""))
  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,.3))
  legend(list(x=2,y=8), legend = leg.txt, col=2:3, pch=1:2,
         lty=1, merge=TRUE)#, trace=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 &lt;- expression(plain(sin) * phi,  paste("cos", phi))# 2 ways[2种方式]
utils::str(legend(-3, .9, ex.cs1, lty=1:2, plot=FALSE,
           adj = c(0, .6)))# adj y ![形容词&#376;!]
legend(-3, .9, ex.cs1, lty=1:2, col=2:3,    adj = c(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)[#“填充”框 - 更多看到的例子(plot.factor)]
op &lt;- 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' :[#使用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 &amp; 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")

转载请注明:出自 生物统计家园网(http://www.biostatistic.net)。


注:
注1:为了方便大家学习,本文档为生物统计家园网机器人LoveR翻译而成,仅供个人R语言学习参考使用,生物统计家园保留版权。
注2:由于是机器人自动翻译,难免有不准确之处,使用时仔细对照中、英文内容进行反复理解,可以帮助R语言的学习。
注3:如遇到不准确之处,请在本贴的后面进行回帖,我们会逐渐进行修订。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|小黑屋|生物统计家园 网站价格

GMT+8, 2025-1-22 23:35 , Processed in 0.030202 second(s), 16 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表