在 R 中添加图例
图例可用于向绘图添加更多信息并增强用户可读性。它涉及标题、索引的创建、绘图框的放置,以便更好地理解绘制的图形。内置的 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
在正常可视化我们的绘图后,要向其添加图例,仅将具有所需值的适当参数提供给 legend()函数。
例子:
R
# declaring the data to plot
x<-1:10
y=x^1/2
z= x^2
# plotting x and y coordinate
# line
plot(x, y, col="blue")
# adding another line on the
# coordinates involving y and z
lines(z, y ,col="red")
# Adding a legend to the graph
# defining the lines
legend(2, 4, legend=c("Equation 1", "Equation 2"),
fill = c("blue","red")
)
R
# declaring the data to plot
x<-1:10
y=x^1/2
z= x^2
# plotting x and y coordinate line
plot(x, y, col="blue")
# adding another line on the
# coordinates involving y and z
lines(z, y ,col="red")
# Adding a legend to the graph
# defining the lines
legend(x = "topleft", box.col = "brown",
bg ="yellow", box.lwd = 2 , title="EQUATIONS",
legend=c("Equation 1", "Equation 2"),
fill = c("blue","red"))
R
# declaring the data to plot
x<-20:1
y=x
z= x*(1/4)
# plotting x and y coordinate line
plot(x, y, lty = 4,col="blue")
# adding another line on the
# coordinates involving y and z
lines(y, z ,lty = 6,col="orange")
# Adding a legend to the graph
# defining the lines
legend(x = "topleft", lty = c(4,6), text.font = 4,
col= c("blue","orange"),text.col = "blue",
legend=c("Equation 1", "Equation 2"))
输出
可以自定义图表中的图例框以满足要求,以便在传达更多信息的同时为图表增添美感。下面给出了可以自定义的图例属性:
- 标题:图例框的标题,可以声明以了解索引表示的内容
- position :图例框位置的指示符;可以有可能的选项:“bottomright”、“bottom”、“bottomleft”、“left”、“topleft”、“top”、“topright”、“right”和“center”
- bty (默认: o) :包围图例的框的类型。可以使用不同类型的字母,其中盒子形状等同于字母形状。例如,“n”可用于无框。
- bg:可以为图例框指定背景颜色
- box.lwd : 图例框线宽的指示符
- box.lty : 图例框线型指示符
- box.col :图例框线条颜色的指示符
将适当的值应用于这些属性,然后将它们传递给 legend()函数,即可实现所需的自定义。
例子:
电阻
# declaring the data to plot
x<-1:10
y=x^1/2
z= x^2
# plotting x and y coordinate line
plot(x, y, col="blue")
# adding another line on the
# coordinates involving y and z
lines(z, y ,col="red")
# Adding a legend to the graph
# defining the lines
legend(x = "topleft", box.col = "brown",
bg ="yellow", box.lwd = 2 , title="EQUATIONS",
legend=c("Equation 1", "Equation 2"),
fill = c("blue","red"))
输出
还可以使用以下属性自定义图例函数的文本以获得更好的样式:
- text.font:一个数值,表示图例文本的字体样式。它具有以下值:(1 - 正常 2 - 粗体,3 - 斜体,4 - 粗体和斜体)
- text.col :用于指示用于编写图例文本的文本颜色
- 边框(默认:黑色):图例框内框的边框颜色指示符
- fill_color :用于填充框的颜色
例子:
电阻
# declaring the data to plot
x<-20:1
y=x
z= x*(1/4)
# plotting x and y coordinate line
plot(x, y, lty = 4,col="blue")
# adding another line on the
# coordinates involving y and z
lines(y, z ,lty = 6,col="orange")
# Adding a legend to the graph
# defining the lines
legend(x = "topleft", lty = c(4,6), text.font = 4,
col= c("blue","orange"),text.col = "blue",
legend=c("Equation 1", "Equation 2"))
输出