增加R中直方图图例上的彩色方块的大小

时间:2012-11-30 11:15:52

标签: r

我试图在R中的直方图上增加图例中彩色方块的大小 - 当我输出PDF时,它们太小,因此很难区分颜色。我搜索了谷歌,R-help Nabble论坛和这个地方,都无济于事。我还尝试了图例文档中的几个命令。

我需要在legend()函数中使用什么来增加它们?是否可以去除每个彩色方块周围的黑色边框以方便观看?

这是我的例子:

a<-c(1,1,2,3,3,3,3,4,54,56,2,23,1,3,23)
hist(a) 
graphics::legend(x=-1,y=10,c(">0%",">20%",">40%",">60%",">80%"),
       x.intersp=1,y.intersp=2,cex=1, bty="n",
       fill=c("black","gray50","gray70","gray85","white"))

enter image description here

我希望更改图例中框的大小?


解决方案:来自@Ben Bolker

添加到图例功能

上方的脚本中
> source("http://www.math.mcmaster.ca/bolker/R/misc/legendx.R")

然后添加

> box.cex=c(2,2)

在图例功能

3 个答案:

答案 0 :(得分:6)

我攻击了legend函数的源,以允许box.cex参数指定框的相对x和y维度。这并不完美 - 如果扩展足够大,则必须调整y.intersp以防止填充框重叠。

source("http://www.math.mcmaster.ca/bolker/R/misc/legendx.R")
a<-c(1,1,2,3,3,3,3,4,54,56,2,23,1,3,23)
cex <- 1
hist(a)
legend("topright",c(">0%",">20%",">40%",">60%",">80%"),
       bty="n",
       fill=c("black","gray50","gray70","gray85","white"),
       box.cex=c(3,3),
       y.intersp=2.8)

enter image description here

答案 1 :(得分:6)

您可以使用非常粗的线条,矩形末端。

plot( 1, type = "n", axes = FALSE, xlab = "", ylab = "" ) # Empty plot
par( lend = 1 ) # Rectangular line endings
legend(
  "topleft",
  c( "Red", "Black" ),
  col = c("red", "black"),
  lty = 1, lwd = 10
)

enter image description here

答案 2 :(得分:2)

很难混合选项填充并更改框的大小。

但我们可以选择 pt.cex pch ,而不使用填充选项。

a<-c(1,1,2,3,3,3,3,4,54,56,2,23,1,3,23)
par(mfrow=c(1,2)) 
hist(a) 

graphics::legend(x=10,y=10,c(">0%",">20%",">40%",">60%",">80%"), 
                 x.intersp=1,y.intersp=1,cex=c(1),bty="n",
                 fill=c("black","gray50","gray70","gray85","white"),
             #    pch=c(24,22,21,23,25),
                 pt.cex = c(2,2,2,2,5), 
                 lwd=1.5,title='Histo fill option') 

hist(a) 

graphics::legend(x=10,y=10,c(">0%",">20%",">40%",">60%",">80%"), 
       x.intersp=1,y.intersp=1,cex=c(1),bty="n",
       pch=c(24,22,21,23,25),
       pt.cex = c(1,2,3,3,4), 
       lwd=1.5, title = 'Histo whithout fill ') 

enter image description here