How to scale the legend box or increase the font size of the legend box in R

I am using the following code to plot some of the 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))

      

Examples of Gamma distributions

As you can see, the legend box is too large. I used cex = 0.5 to shorten the legend fields in the first two subgraphs. However, when compressing the boxes, it is difficult to read the texts. Hence,

Q1: How to enlarge the text in the legend field?

Another problem is that as the font size of the labels in the top left column increases, it goes out of bounds. Hence,

Q2: How can I increase the left margin so it doesn't chop off my head from my y-mark?

Finally, the aspect ratio is weird. Hence,

Q3: How to manually adjust the width and height of the whole chart? or is there a way to reduce the space between two lines of subgraphs?

Thank!

0


source to share


2 answers


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

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

      



This sets the bottom, left, top, and right margins. Just increase the left margin (to show the Y-mark) and decrease the top and bottom margins (to get fewer spaces).

This will not answer your Q1. But since your numbers will be larger, because you have less free space, your legend is more likely to fit.

+1


source


If I increase the font size, I think the text will no longer fit the legend field. So you need to tweak cex until you can read the text. A script is running on my screen. If you don't have enough space within the plot, you can place the legend outside the plot.

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

      



enter image description here

+1


source







All Articles