简体   繁体   English

调整R中图例的字体

[英]Adjust the font of legend in R

I use legend() to produce a legend shown below我使用legend()来生成如下所示的图例

一个情节的传说

The text lies beyond the plot box.文本位于图框之外。 I tried to use cex = to adjust the box, however, it can only adjust the size of the whole box, but have nothing to do with the text font.我试过用cex =来调整框,但是只能调整整个框的大小,与文字字体无关。

Is there anyway to make the text font smaller?有没有办法使文本字体更小?

Here is my sample code:这是我的示例代码:

legend("bottomleft", legend = c("Simulated", "Estimated/Predicted    
       Median", "95% Credit Intervals"),
       col = c("gray35", "red", "red"), lty = c(1, 1, 2), 
               lwd = c(3, 2, 1),
       text.font = 3, inset=.02, bg='gray90')

You can set graphical parameters by applying par() .您可以通过应用par()来设置图形参数。 For example:例如:

plot(c(1:4), c(1:4), type  = 'l')    
par(cex = 1) #set legend font to 1
legend("topleft", legend="a line", lty = 1)

If you set bty="n" it won't draw a box如果你设置bty="n"它不会画一个框

legend("bottomleft", legend = c("Simulated", "Estimated/Predicted    
   Median", "95% Credit Intervals"),
   col = c("gray35", "red", "red"), lty = c(1, 1, 2), 
   lwd = c(3, 2, 1),
   text.font = 3, inset=.02, bg='gray90',bty="n")

Try to hold the pt.cex parameter to 1, while trying different values for cex inside the legend call.尝试将pt.cex参数保持为 1,同时在图例调用中尝试不同的cex值。 pt.cex control the size of points and lines of the legend. pt.cex控制图例的点和线的大小。

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

## I tried to feed cex with 1.1 and 0.4. The font size changes while the lines remain unchanged.

legend("bottomleft", legend = c("Simulated", "Estimated/Predicted    
   Median", "95% Credit Intervals"),
   col = c("gray35", "red", "red"), lty = c(1, 1, 2), 
   lwd = c(3, 2, 1),
   text.font = 3, inset=.02, bg='gray90', pt.cex = 1, cex = 0.4) 

在此处输入图片说明

As you can see, the size size of the font change while the lines remain almost the same.如您所见,字体的大小发生了变化,而线条几乎保持不变。 Try play with them until you do not find the correct proportions for your plot.尝试与它们一起玩,直到找不到适合您的情节的正确比例。

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

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