调整R中图例的字体

时间:2016-04-22 15:07:14

标签: r plot legend

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

Legend of a plot

文字位于情节框之外。我尝试使用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')

3 个答案:

答案 0 :(得分:2)

您可以应用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)

答案 1 :(得分:0)

如果您设置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")

答案 2 :(得分:0)

尝试将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) 

enter image description here

正如您所看到的,字体的大小会在线条保持几乎相同时发生变化。尝试使用它们,直到找不到正确的比例。