调整R中图例的字体

Adjust the font of legend in R

我使用 legend() 生成如下所示的图例

文本位于绘图框之外。我试过用cex =调整方框,但是只能调整整个方框的大小,与文字字体无关。

有什么方法可以把文字字体变小吗?

这是我的示例代码:

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"它不会画一个方框

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")

尝试将 pt.cex 参数保持为 1,同时尝试在图例调用中为 cex 设置不同的值。 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) 

如您所见,字体大小发生变化,而线条几乎保持不变。尝试和他们一起玩,直到你找不到适合你情节的正确比例。

您可以通过应用par()设置图形参数。例如:

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