4

我正在尝试增加 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"))

在此处输入图像描述

我想更改图例中框的大小?


解决方案:来自@Ben Bolker

添加到图例函数上方的脚本

> source("http://www.math.mcmaster.ca/bolker/R/misc/legendx.R")

然后加

> box.cex=c(2,2)

在图例函数中

4

3 回答 3

7

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

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
)

在此处输入图像描述

于 2012-11-30T14:46:31.543 回答
7

我破解了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)

在此处输入图像描述

于 2012-11-30T13:55:42.447 回答
2

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

但是我们可以有一些选项 pt.cexpch没有填充选项。

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

在此处输入图像描述

于 2012-11-30T13:14:16.297 回答