如何在R中的图例框中缩放图例框或放大字体大小

时间:2015-02-28 21:07:55

标签: r margin font-size

我使用以下代码绘制一些伽玛分布。

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

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

Q1:如何放大图例框中的文字?

另一个问题是,当我增加左上图中标签的字体大小时,它会超出边距。因此,

Q2:我怎样才能增加左边距,这样就不会将头部从我的y标签上砍掉?

最后,纵横比很奇怪。因此,

问题3:如何手动设置整个图形的宽度和高度?或者有没有办法缩小两行子图之间的空白区域?

谢谢!

2 个答案:

答案 0 :(得分:1)

对于Q2和Q3,您可以在par函数中使用mar参数,例如:

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

设置底部,左侧,顶部和右侧边距。 只需放大左边距(以显示您的Y标签)并缩小顶部和底部边距(以减少白色空间)。

这不能回答你的Q1。但是因为你的空白较少,你的数字会更大,你的传奇更容易适应。

答案 1 :(得分:1)

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

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