将附加图例放置在R中现有图例的底部

我有以下传说:

如何找到带有“均值和标清”的中心文本
它可以放在“— emp”下.

生成的代码是这样的:

L = list(bquote(Em.Mean ==.(new_avg)),
    bquote(Em.SD==.(new_std)), bquote(Th.Mean ==.(theor_avg)),
    bquote(Th.SD==.(theor_sd)))

legend("topright", c(kids,"emp."), cex=0.7, bty="n", col=c(cm.colors(6), "red"),
    pch=c(rep(19, 6), -5), lty = c(0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0), )

# How can I locate this    
legend("topcenter", cex=0.5, bty="n", legend=sapply(L, as.expression))

最佳答案 图例坐标可以分配给对象.然后可以使用这些坐标为您添加第二个图例.例如,

x <-10:1
y <-11:20
plot(x,y,type="h")
my.legend.size <- legend("topright",c("Series1","Series2","Series3"))
> my.legend.size
$rect
$rect$w
[1] 1.599609

$rect$h
[1] 1.506977

$rect$left
[1] 8.760391

$rect$top
[1] 20.36


$text
$text$x
[1] 9.266641 9.266641 9.266641

$text$y
[1] 19.98326 19.60651 19.22977

使用$rect $left,$rect $top和$rect $h,您可以为第二个图例提供新坐标.

#second legend
legend(x=my.legend.size$rect$left,y=my.legend.size$rect$top-    
my.legend.size$rect$h,c("Series4","Series5","Series6"))

我注意到你的第二个传奇的宽度比第一个更大.这里的技巧是获取第二个图例的坐标但不使用打印,plot = FALSE第二个图例的x坐标是my.legend.size2 $rect $left.

x <-10:1
y <-11:20
plot(x,y,type="h")
my.legend.size <-legend("topright",c("Series1","Series2","Series3"))
my.legend.size2 <-legend("right",c("Long series name","Series5","Series6"),plot = FALSE)
legend(x=my.legend.size2$rect$left,y=my.legend.size$rect$top-
my.legend.size$rect$h,c("Long series name","Series5","Series6"))

PS:我已经在传说周围留下了方框来表明展示位置是完美的,但你可以使用bty =“n”删除它们

点赞