在 R 编程中以图形形式描述图表的各个部分 – legend()函数
R 编程语言中的legend()函数用于将图例添加到现有绘图中。图例被定义为描述图表每个部分的图表区域。图例图用于以图形形式显示统计数据。
Syntax: legend(x, y, legend, fill, col, bg, lty, cex, title, text.font, bg)
Parameters:
- x and y: These are co-ordinates to be used to position the legend
- legend: Text of the legend
- fill: Colors to use for filling the boxes of legend text
- col: Colors of lines
- bg: It defines background color for the legend box.
- title: Legend title (optional)
- text.font: An integer specifying the font style of the legend (optional)
- Returns: Legend plot
R – 图例() 示例
示例 1: R 中 legend()函数的基本示例
R
# Generate some data
x <- 1:8;
y1 = x; y2 = 2 * y1
plot(x, y1, type = "b", pch = 22,
col = "green",
xlab = "X", ylab = "Y")
# Add a line
lines(x, y2, pch = 18, col = "darkgreen",
type = "b", lty = 2)
# Add a legend
legend(1, 50, legend = c("Legend Line 1", "Legend Line 2"),
col = c("green", "darkgreen"), lty = 2:3, cex = 0.6)
R
makePlot<-function(){
x<-1:10;
y1 = x * x; y2 = 2 * y1
plot(x, y1, type = "b", pch = 19,
col = "green",
xlab = "X", ylab = "Y")
lines(x, y2, pch = 22, col = "darkgreen",
type = "b", lty = 6)
}
makePlot()
# Add a legend to the plot
legend(1, 95, legend = c(" Legend Line 1", "Legend Line 2"),
col = c("green", "darkgreen"), lty = 1:2, cex = 0.9,
title = "Line types", text.font = 6, bg = 'gray')
R
makePlot<-function(){
x<-1:10;
y1 = x * x; y2 = 2 * y1
plot(x, y1, type = "b", pch = 22,
col = "green",
xlab = "x", ylab = "y")
lines(x, y2, pch = 18, col = "darkgreen",
type = "b", lty = 4)
}
# Change the border
makePlot()
legend(1, 100, legend = c("Legend Line 1", "Legend Line 2"),
col = c("green", "darkgreen"), lty = 1:2, cex = 0.8,
box.lty = 4, box.lwd = 2, box.col = "green")
R
makePlot<-function(){
x<-1:10;
y1 = x * x; y2 = 2 * y1
plot(x, y1, type = "b", pch = 22,
col = "green",
xlab = "x", ylab = "y")
lines(x, y2, pch = 18, col = "darkgreen",
type = "b", lty = 4)
}
# Remove legend border using box.lty = 0
makePlot()
legend(2, 100, legend = c("Legend Line 1",
"Legend Line 2"),
col = c("green", "darkgreen"),
lty = 1:2, cex = 0.8, box.lty = 0)
输出:
示例 2:添加图例框的标题、文本字体和背景颜色
R
makePlot<-function(){
x<-1:10;
y1 = x * x; y2 = 2 * y1
plot(x, y1, type = "b", pch = 19,
col = "green",
xlab = "X", ylab = "Y")
lines(x, y2, pch = 22, col = "darkgreen",
type = "b", lty = 6)
}
makePlot()
# Add a legend to the plot
legend(1, 95, legend = c(" Legend Line 1", "Legend Line 2"),
col = c("green", "darkgreen"), lty = 1:2, cex = 0.9,
title = "Line types", text.font = 6, bg = 'gray')
输出:
在这里,legend()函数用于为绘图添加图例,makePlot()函数用于操作字体、背景颜色。
示例 3:创建图例框边框的另一个示例
Syntax: legendx, y, fill, col, bg, lty, cex=0.8, box.lty, box.lwd, box.col)
Parameters:
box.lty, box.lwd and box.col: line type, width and color for the legend box border, respectively.
R
makePlot<-function(){
x<-1:10;
y1 = x * x; y2 = 2 * y1
plot(x, y1, type = "b", pch = 22,
col = "green",
xlab = "x", ylab = "y")
lines(x, y2, pch = 18, col = "darkgreen",
type = "b", lty = 4)
}
# Change the border
makePlot()
legend(1, 100, legend = c("Legend Line 1", "Legend Line 2"),
col = c("green", "darkgreen"), lty = 1:2, cex = 0.8,
box.lty = 4, box.lwd = 2, box.col = "green")
输出:
示例 4:下图是通过在 legend()函数中使用 box.lty = 0 来移除图例边框的图示
Syntax: legendx, y, fill, col, bg, lty, cex=0.8, box.lty=0)
Parameter: box.lty: Box line width
R
makePlot<-function(){
x<-1:10;
y1 = x * x; y2 = 2 * y1
plot(x, y1, type = "b", pch = 22,
col = "green",
xlab = "x", ylab = "y")
lines(x, y2, pch = 18, col = "darkgreen",
type = "b", lty = 4)
}
# Remove legend border using box.lty = 0
makePlot()
legend(2, 100, legend = c("Legend Line 1",
"Legend Line 2"),
col = c("green", "darkgreen"),
lty = 1:2, cex = 0.8, box.lty = 0)
输出:
在示例 3 和示例 4 中,box.lty、box.lwd 和 box.col 可用于修改图例框边框的线型、宽度和颜色,分别用于修改参数。