简体   繁体   English

更改图例中的字体大小

[英]Change font-size in legend

I have a legend in my plot, but I'm trying to increase the font size so it fit the legend-box.我的情节中有一个图例,但我正在尝试增加字体大小以使其适合图例框。 When I try to increase the cex as defined below.当我尝试增加如下定义的cex The box gets bigger, while the text is still small.盒子变大了,而文本仍然很小。

Code:代码:

legend(0,16, c("Available vCPUs", "Added vCPUs (1 per iteration ) "),
 col=c('red', 'black'), cex=0.39, lty=1:1, lwd=2)

Excerpt from plot:情节摘录:

在此处输入图片说明

First approach:第一种方法:

Try to set the font size before to plot the legend.尝试在绘制图例之前设置字体大小。

 x <- y <- rnorm(100, 0, 1)
 plot(x, y, type = "n")

## here you set the font size default to `x`, in this example 0.5
## save defaults in `op`

 op <- par(cex = 0.5)

 legend("topright", legend = "foo legend", pch = 1, bty = "n")

在此处输入图片说明

## here you set cexto 1.5
## save new defaults in `op`

 op <- par(cex = 1.5)

 legend("topright", legend = "foo legend", pch = 1, bty = "n")

在此处输入图片说明

Second approach:第二种方法:

Holding the pt.cex parameter to 1, while trying different values for cex inside the legend call.拿着pt.cex参数为1,而尝试不同的值cex传说调用 Remember to delete op .记得删除op

x <- rnorm(100, 10, 4)
y <- rnorm(100, 10, 4)
plot(x, y, type = "n")

## I tried to feed cex with 1.5 and 0.5. The font size changes while the points remain unchanged.

legend("topleft", "Legend", cex=0.5, pch=1, pt.cex = 1)

在此处输入图片说明

You can use cex to determine font size, use bty='n' to indicate no lines around the legend, then draw a rectangle separately on the graph with rect().可以用cex确定字体大小,用bty='n'表示图例周围没有线条,然后用rect()在图形上单独画一个矩形。 For example:例如:

with(data, legend(-10,7, legend=c("Name_of_Legend"), bty = 'n', col=c("red"), lty=0, pch=20, cex=0.75))
with(data, rect(-10,6.2,-3,7))

I think you can try using the y.intersp in legend, when the intervals between different text lines are reduced, you could increase text size without changing the size of legend box.我认为您可以尝试使用y.intersp中的y.intersp ,当不同文本行之间的间隔减少时,您可以在不更改图例框大小的情况下增加文本大小。

legend(0,16, c("Available vCPUs","Added vCPUs (1 per iteration )
"),col=c('red','black'),cex=0.39,lty=1:1,lwd=2, y.intersp = 0.3)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM