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

时间:2012-05-15 10:26:18

标签: r plot

我有以下传说:

enter image description here

如何找到带有“均值和标清”的中心文字 它可以正好放在“--- 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))

1 个答案:

答案 0 :(得分:2)

可以将

legend坐标分配给对象。然后可以使用这些坐标为您添加第二个图例。例如,

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

enter image description here

我注意到你的第二个传奇的宽度比第一个更大。这里的技巧是获取第二个图例的坐标,但不使用,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"))

enter image description here

PS:我已经在传说周围留下了方框以显示展示位置是完美的,但您可以使用bty = "n"

删除它们