如何在R中的图例框中缩放图例框或扩大字体大小 [英] How to scale legend box or enlarge font size in the legend box in R

查看:2784
本文介绍了如何在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))

如您所见,图例框的大小太大.我使用cex = 0.5来缩小前两个子图中的图例框.但是,缩小框会使阅读文本变得困难.因此,

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,

问题1:如何放大图例框中的文本?

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:如何增加左边距,以免将头从我的y标签上砍掉?

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:如何手动设置整个图形的宽度和高度?还是有什么方法可以缩小两行子图之间的空白?

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?

谢谢!

推荐答案

如果您增加字体大小,我认为文本将不再适合图例框.因此,在阅读文本之前,您需要调整cex.在我的屏幕上,以下脚本有效.如果地块内没有足够的空间,则可以将de legend放置在地块区域之外.

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

这篇关于如何在R中的图例框中缩放图例框或扩大字体大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆