更改 Base R 图中的图例大小
在本文中,我们将研究在 R 编程语言中更改图中图例大小的方法。
要更改图的图例大小,用户需要使用图例函数的cex参数并根据用户要求指定其值,cex 的值大于 1 将增加图中的图例大小和 cex 的值小于 1 将减小图中图例的大小。
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 with legend text
- col: Colors of lines
- bg: It defines background color for the legend box
- cex: Used for scaling
- title: Legend title (optional)
- text.font: An integer specifying the font style of the legend (optional)
Returns: Legend plot
cex是一个数字,表示绘图文本和符号相对于默认值的缩放量。 1=默认值,1.5 大 50%,0.5 小 50%,依此类推。
示例 1:
R
x1 <- c(1,8,5,3,8,7)
y1 <- c(4,6,3,8,2,7)
plot(x1,y1,cex=.8,pch=1,col="red")
x2<-c(4,5,8,6,4)
y2<-c(9,8,2,3,1)
x3<-c(2,1,6,7,4)
y3<-c(7,9,1,5,2)
points(x2,y2,cex=.8,pch=2,col="blue")
points(x3,y3,cex=.8,pch=3,col="green")
legend("topright",c("gfg1","gfg2","gfg3"),
cex=0.5,col=c("red","blue","green"),
pch=c(1,2,3))
R
gfg_data <- matrix(c(1,2,3,4,5,6,7,8,9,10), ncol = 5)
colnames(gfg_data) <- paste0("Gfg", 1:5)
rownames(gfg_data) <- c('A','B')
gfg_data
barplot(gfg_data,
col = 1:nrow(gfg_data))
legend("topright",
legend = rownames(gfg_data),
pch = 15,
col = 1:nrow(gfg_data),cex=2.5)
输出:
示例 2:
电阻
gfg_data <- matrix(c(1,2,3,4,5,6,7,8,9,10), ncol = 5)
colnames(gfg_data) <- paste0("Gfg", 1:5)
rownames(gfg_data) <- c('A','B')
gfg_data
barplot(gfg_data,
col = 1:nrow(gfg_data))
legend("topright",
legend = rownames(gfg_data),
pch = 15,
col = 1:nrow(gfg_data),cex=2.5)
输出: