更改R中图例的边距 [英] Altering the margins of a legend in R

查看:354
本文介绍了更改R中图例的边距的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对R很陌生,所以我在这里有点挣扎,但没有找到解决我问题的答案.

我正在尝试在R中生成一个简单的条形图,并且已经使用las=2将我的x轴变量标签设置为垂直.然后,我使用par(mar=c(20,15,5,3))par(mgp=c(6,1,0))更改了条形图的边距,以使标签不会与xlab标签重叠.

我想为此添加一个图例,但是我采用了图例本身的边距尺寸,因此它看起来太大而不合适.我尝试使用cex,但这只会影响图例中的文本.无论如何,我是否可以独立更改图例边距(或图形边距)?

这是我编写的代码:

par(mar=c(20,15,5,3))
par(mgp=c(6,1,0))
par(xpd=TRUE)
barplot(
  names.arg=c("Africa", "Central America, South America, Caribbean", 
              "Middle East", "Central and Eastern Europe", 
              "South and East Asia"),
  cex.names=0.8, las=2, t(YLL), 
  ylab="Percentage (%)", ylim=c(0,100), main="", beside=TRUE, 
  col= c("green4", "orange"),xlab="Regions", mar=c(20,15,5,3)
)
legend(
  10, 100, 
  legend=c("Communicable diseases", "Communicable diseases"), 
  fill= c("green4", "orange"), cex=0.7
)

非常感谢您的帮助.

解决方案

您还可以使用legend函数的参数:y.interspx.intersptext.width来减小图例的大小.

这里有个例子:

set.seed(55) # Set the seed of R‘s random number generator
x <- 1:10
y <- runif(10, min=0, max=100) # Generating random numbers
z <- runif(10, min=0, max=100) # Generating random numbers
plot(x,y, type="l", ylim=c(0,100), ylab="y and z")
par(new=TRUE)
plot(x,z, type="l", col="red", ylim=c(0,100), ylab=NA, xaxt='n', yaxt='n')
legend("topright", c("y","z"), lty="solid", col=c("black", "red"))

和修改legend函数的相同代码:

set.seed(55) # Set the seed of R‘s random number generator
x <- 1:10
y <- runif(10, min=0, max=100) # Generating random numbers
z <- runif(10, min=0, max=100) # Generating random numbers
plot(x,y, type="l", ylim=c(0,100), ylab="y and z")
par(new=TRUE)
plot(x,z, type="l", col="red", ylim=c(0,100), ylab=NA, xaxt='n', yaxt='n')
legend("topright", c("y","z"), lty="solid", col=c("black", "red"), y.intersp=0.5,x.intersp=0.5,text.width=0.1)

I am very new to R, so I'm struggling here a bit and I haven't found an answer to my problem.

I'm trying to produce a simple bar-chart in R, and I have set my x-axis variable labels to be vertical, using las=2. I then changed the margins for the barplot so that the labels would not overlap the xlab label, using par(mar=c(20,15,5,3)) and par(mgp=c(6,1,0)).

I would like to add a legend to this, but the one I have has adopted the margin dimensions of the graph itself, so that it appears too big and does not fit. I tried using cex but that only affects the text in the legend. Is there anyway for me to change the legend margins (or the graph margins) independently?

Here's what I have coded:

par(mar=c(20,15,5,3))
par(mgp=c(6,1,0))
par(xpd=TRUE)
barplot(
  names.arg=c("Africa", "Central America, South America, Caribbean", 
              "Middle East", "Central and Eastern Europe", 
              "South and East Asia"),
  cex.names=0.8, las=2, t(YLL), 
  ylab="Percentage (%)", ylim=c(0,100), main="", beside=TRUE, 
  col= c("green4", "orange"),xlab="Regions", mar=c(20,15,5,3)
)
legend(
  10, 100, 
  legend=c("Communicable diseases", "Communicable diseases"), 
  fill= c("green4", "orange"), cex=0.7
)

I will really appreciate the help, thanks.

解决方案

You can also use the arguments of legend function: y.intersp, x.intersp and text.width for reduce the size of the legend.

Here an example:

set.seed(55) # Set the seed of R‘s random number generator
x <- 1:10
y <- runif(10, min=0, max=100) # Generating random numbers
z <- runif(10, min=0, max=100) # Generating random numbers
plot(x,y, type="l", ylim=c(0,100), ylab="y and z")
par(new=TRUE)
plot(x,z, type="l", col="red", ylim=c(0,100), ylab=NA, xaxt='n', yaxt='n')
legend("topright", c("y","z"), lty="solid", col=c("black", "red"))

And same code modifiying the legend function:

set.seed(55) # Set the seed of R‘s random number generator
x <- 1:10
y <- runif(10, min=0, max=100) # Generating random numbers
z <- runif(10, min=0, max=100) # Generating random numbers
plot(x,y, type="l", ylim=c(0,100), ylab="y and z")
par(new=TRUE)
plot(x,z, type="l", col="red", ylim=c(0,100), ylab=NA, xaxt='n', yaxt='n')
legend("topright", c("y","z"), lty="solid", col=c("black", "red"), y.intersp=0.5,x.intersp=0.5,text.width=0.1)

这篇关于更改R中图例的边距的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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