正如@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')