图例位置,ggplot,相对于绘图区域。

74

这里的问题有点显而易见。我希望将图例放置(锁定)在“绘图区域”的左上角。使用c(0.1,0.13)等选项并不可行,原因有很多。

是否有一种方法可以更改坐标的参考点,使它们相对于绘图区域呢?

mtcars$cyl <- factor(mtcars$cyl, labels=c("four","six","eight"))
ggplot(mtcars, aes(x=wt, y=mpg, colour=cyl)) + geom_point(aes(colour=cyl)) + 
opts(legend.position = c(0, 1), title="Legend placement makes me sad")

在此输入图片描述

干杯


1
“plot region”是什么意思?是指被灰色填充的区域吗? - kohske
1
@kohske 是的,OP基本上想要能够确定 legend.position 的正确坐标,以将图例放置在“数据区域”或灰色区域的角落。正如我在聊天中提到的那样,我怀疑像您这样的人可能需要权衡一下网格解决方案。 - joran
@joran 那么,这是默认行为,你只需要设置正确的对齐方式即可。 - kohske
@kohske 你知道吗,我一直以为它是基于设备地区的。每天都会学到新东西... - joran
@joran 或许在0.9或0.89之前那是默认行为,不过我不确定。 - kohske
4个回答

74

更新: opts 已被弃用,请使用 theme 替代,如此答案所述。

为了更详细地解释kohske的答案,使下一个遇到这个问题的人更容易理解。

mtcars$cyl <- factor(mtcars$cyl, labels=c("four","six","eight"))
library(gridExtra)

a <- ggplot(mtcars, aes(x=wt, y=mpg, colour=cyl)) + geom_point(aes(colour=cyl)) + 
opts(legend.justification = c(0, 1), legend.position = c(0, 1), title="Legend is top left")
b <- ggplot(mtcars, aes(x=wt, y=mpg, colour=cyl)) + geom_point(aes(colour=cyl)) + 
opts(legend.justification = c(1, 0), legend.position = c(1, 0), title="Legend is bottom right")
c <- ggplot(mtcars, aes(x=wt, y=mpg, colour=cyl)) + geom_point(aes(colour=cyl)) + 
opts(legend.justification = c(0, 0), legend.position = c(0, 0), title="Legend is bottom left")
d <- ggplot(mtcars, aes(x=wt, y=mpg, colour=cyl)) + geom_point(aes(colour=cyl)) + 
opts(legend.justification = c(1, 1), legend.position = c(1, 1), title="Legend is top right")

grid.arrange(a,b,c,d)

在此输入图片描述


72

我一直在寻找类似的答案。但发现opts函数不再是ggplot2包的一部分。在搜索了一段时间后,我发现可以使用theme来完成与opts类似的事情。因此,编辑这个帖子,以便减少其他人的时间。

以下是由nzcoops编写的类似代码。

mtcars$cyl <- factor(mtcars$cyl, labels=c("four","six","eight"))
library(gridExtra)

a <- ggplot(mtcars, aes(x=wt, y=mpg, colour=cyl)) + geom_point(aes(colour=cyl)) + labs(title = "Legend is top left") + 
theme(legend.justification = c(0, 1), legend.position = c(0, 1))

b <- ggplot(mtcars, aes(x=wt, y=mpg, colour=cyl)) + geom_point(aes(colour=cyl)) + labs(title = "Legend is bottom right") +
theme(legend.justification = c(1, 0), legend.position = c(1, 0))

c <- ggplot(mtcars, aes(x=wt, y=mpg, colour=cyl)) + geom_point(aes(colour=cyl)) + labs(title = "Legend is bottom left") +
theme(legend.justification = c(0, 0), legend.position = c(0, 0))

d <- ggplot(mtcars, aes(x=wt, y=mpg, colour=cyl)) + geom_point(aes(colour=cyl)) + labs(title = "Legend is top right") +
theme(legend.justification = c(1, 1), legend.position = c(1, 1))

grid.arrange(a,b,c,d)

这段代码将给出完全相似的图形。


58
更新:`opts`已被弃用。请改用`theme`,如这个答案所述。
默认情况下,指南的放置基于绘图区域(即灰色填充的区域),但对齐方式是居中的。 因此,您需要设置左上对齐方式:
ggplot(mtcars, aes(x=wt, y=mpg, colour=cyl)) + geom_point(aes(colour=cyl)) + 
  opts(legend.position = c(0, 1), 
       legend.justification = c(0, 1), 
       legend.background = theme_rect(colour = NA, fill = "white"),
       title="Legend placement makes me happy")

enter image description here

如果您想将指南放置在整个设备区域上,可以调整gtable输出:
p <- ggplot(mtcars, aes(x=wt, y=mpg, colour=cyl)) + geom_point(aes(colour=cyl)) + 
  opts(legend.position = c(0, 1), 
       legend.justification = c(0, 1), 
       legend.background = theme_rect(colour = "black"),
       title="Legend placement makes me happy")

gt <- ggplot_gtable(ggplot_build(p))
nr <- max(gt$layout$b)
nc <- max(gt$layout$r)
gb <- which(gt$layout$name == "guide-box")
gt$layout[gb, 1:4] <- c(1, 1, nr, nc)
grid.newpage()
grid.draw(gt)

enter image description here


谢谢,不是“靠左侧”,但与两侧等距离会比较好 :) - nzcoops
16
opts已经被弃用,请使用theme代替。 - papirrin
这会产生一个错误:'找不到函数"opts"'。 - Luís de Sousa

22

在上面出色的答案中进行补充,如果您想在图例和框之间添加填充,请使用legend.box.margin

# Positions legend at the bottom right, with 50 padding
# between the legend and the outside of the graph.
theme(legend.justification = c(1, 0), 
    legend.position = c(1, 0),
    legend.box.margin=margin(c(50,50,50,50)))

在撰写本文时,这适用于最新版本的ggplot2(即v2.2.1)。


2
很好的扩展,边距设置得很好!只有一个小建议,如果您想要所有的边距都相同,可以使用rep(50, times=4)来更轻松地调整边距值。 - Mario Reutter

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接