How to scale legend box or enlarge font size in the legend box in R

不羁岁月 提交于 2019-12-08 11:17:26

问题


I am using the following code to plot some gamma distributions.

par(mfrow=c(2, 2))
a = 0.5; b = 2
curve(dgamma(x, a, b), from=0.01, to=0.2, ylab="p(x)", cex.lab=1.5)
b = 1
curve(dgamma(x, a, b), from=0.01, to=0.2, col=2, add=T)
b = 0.5
curve(dgamma(x, a, b), from=0.01, to=0.2, col=4, add=T)
legend("topright", bty="n", lty=1, lwd=1.5, col=c(1,2,4), cex=0.5,ft.cex=1,
       c("a = 0.5, b = 2","a = 0.5, b = 1","a = 0.5, b = 0.5"))

a = 1; b = 2
curve(dgamma(x, a, b), from=0.01, to=4, ylab="p(x)", cex.lab=1.5)
b = 1
curve(dgamma(x, a, b), from=0.01, to=4, col=2, add=T)
b = 0.5
curve(dgamma(x, a, b), from=0.01, to=4, col=4, add=T)
legend("topright", bty="n", lty=1, lwd=1.5, col=c(1,2,4), cex=0.5,
       c("a = 1, b = 2","a = 1, b = 1","a = 1, b = 0.5"))

a = 2; b = 2
curve(dgamma(x, a, b), from=0.01, to=8, ylab="p(x)", cex.lab=1.5)
b = 1
curve(dgamma(x, a, b), from=0.01, to=8, col=2, add=T)
b = 0.5
curve(dgamma(x, a, b), from=0.01, to=8, col=4, add=T)
legend("topright", bty="n", lty=1, lwd=1.5, col=c(1,2,4), 
       c("a = 2, b = 2","a = 2, b = 1","a = 2, b = 0.5"))

a = 20; b = 2
curve(dgamma(x, a, b), from=0.01, to=70, ylab="p(x)", cex.lab=1.5)
b = 1
curve(dgamma(x, a, b), from=0.01, to=70, col=2, add=T)
b = 0.5
curve(dgamma(x, a, b), from=0.01, to=70, col=4, add=T)
legend("topright", bty="n", lty=1, lwd=1.5, col=c(1,2,4), 
       c("a = 20, b = 5","a = 20, b = 1","a = 20, b = 0.2"))
par(mfrow=c(1, 1))

As you can see, the size of the legend box is too big. I used cex=0.5 to shrink the legend boxes in the first two sub-graphs. However, shrinking the boxes makes it hard to read the texts. Hence,

Q1: How can I enlarge the text in the legend box?

Another problem is that as I increase the font size of the labels in the top-left graph, it goes beyond the margin. Hence,

Q2: How can I increase the left margin so it did not chop the head off my y-label?

Finally, the aspect ratio is weird. Hence,

Q3: How can I set the width and height of the entire graph manually? or Is there any way to shrink the white space between the two rows of sub-graphs?

Thanks!


回答1:


For Q2 and Q3 you can use the mar argument in the par function, for example:

   par(mar=c(2,6,2,2), mfrow=c(2, 2))

This sets the bottom, left, top and right margins. Just enlarge the left margin (to show your Y-label) and shrink the top and bottom margins (to get less white space).

This doesn't answer your Q1. But because your figures will be bigger because you have less white space, your legend is more likely to fit.




回答2:


If you increase font size, i think the text will no longer fit the legend box. So you need to adjust cex until you can read your text. On my screen the script below works. If you don't have enough room inside the plot, you could put de legend outside the plot area.

par(mar=c(2.5,5,1,1), mfrow=c(2, 2))
a = 0.5; b = 2
curve(dgamma(x, a, b), from=0.01, to=0.2, ylab="p(x)", cex.lab=1.5)
b = 1
curve(dgamma(x, a, b), from=0.01, to=0.2, col=2, add=T)
b = 0.5
curve(dgamma(x, a, b), from=0.01, to=0.2, col=4, add=T)
legend("topright", bty="n", lty=1, lwd=1.5, col=c(1,2,4), cex=0.7, pt.cex(2), 
       c("a = 0.5, b = 2","a = 0.5, b = 1","a = 0.5, b = 0.5"))

a = 1; b = 2
curve(dgamma(x, a, b), from=0.01, to=4, ylab="p(x)", cex.lab=1.5)
b = 1
curve(dgamma(x, a, b), from=0.01, to=4, col=2, add=T)
b = 0.5
curve(dgamma(x, a, b), from=0.01, to=4, col=4, add=T)
legend("topright", bty="n", lty=1, lwd=1.5, col=c(1,2,4), cex=0.5,
       c("a = 1, b = 2","a = 1, b = 1","a = 1, b = 0.5"))

a = 2; b = 2
curve(dgamma(x, a, b), from=0.01, to=8, ylab="p(x)", cex.lab=1.5)
b = 1
curve(dgamma(x, a, b), from=0.01, to=8, col=2, add=T)
b = 0.5
curve(dgamma(x, a, b), from=0.01, to=8, col=4, add=T)
legend("topright", bty="n", lty=1, lwd=1.5, col=c(1,2,4), 
       c("a = 2, b = 2","a = 2, b = 1","a = 2, b = 0.5"))

a = 20; b = 2
curve(dgamma(x, a, b), from=0.01, to=70, ylab="p(x)", cex.lab=1.5)
b = 1
curve(dgamma(x, a, b), from=0.01, to=70, col=2, add=T)
b = 0.5
curve(dgamma(x, a, b), from=0.01, to=70, col=4, add=T)
legend("topright", bty="n", lty=1, lwd=1.5, col=c(1,2,4), 
       c("a = 20, b = 5","a = 20, b = 1","a = 20, b = 0.2"))
par(mfrow=c(1, 1))



来源:https://stackoverflow.com/questions/28786916/how-to-scale-legend-box-or-enlarge-font-size-in-the-legend-box-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!