简体   繁体   中英

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

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!

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.

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

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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