【问题标题】:How to shrink the inner margins of legend box如何缩小图例框的内边距
【发布时间】:2014-05-19 02:00:27
【问题描述】:

我正在绘制如下图。我用来生成图例的代码是

legend(4, 20, c("Placebo", "Progabide"), lty=1:2, pch=c(1,16), col=1:2, cex=0.8)

问题是内边距(垂直方向)太大,我想减小它。我想缩小内边距的另一种方法是进一步减少“cex”。但随后框中的文本也会变小。有没有办法减少盒子但不减少它的内容。

【问题讨论】:

  • 您可以使用 bty='n' 参数隐藏该框,并使用 rect() 绘制您自己的框。

标签: r legend margins


【解决方案1】:

正如@jbaums 所提到的,这是一个如何使用rect 执行此操作的示例。通过使用默认图例的rect信息,可以确保框位置正确。

plot(x=1:10,y=1:10+rnorm(10), ylim=c(0,11))

# draw default box and store size of default rect in 'a'
# disable this default rect by adding plot=F to legend()
a=legend(x=1,y=9, c("old box", "new box"), lty=1, col=2:1, cex=0.8, y.intersp=0.8,box.col=2)#,plot=F)

# box size reduced by factor 0.75
a=a$rect
mid = a$top - 0.5*a$h
reduction = 0.75

# draw new box
rect(xleft=a$left, ytop=mid+0.5*reduction*a$h, xright=a$left+a$w, ybottom=mid-0.5*reduction*a$h)
# add legend items to new box
legend(x=1,y=9, c("old box", "new box"), lty=1, col=2:1, cex=0.8, y.intersp=0.8, bty='n')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-23
    • 1970-01-01
    • 2015-02-09
    • 2022-01-25
    • 2013-09-06
    相关资源
    最近更新 更多