图例放置,ggplot,相对于绘图区域

时间:2012-05-25 01:15:21

标签: r ggplot2

我认为这里的问题有点明显。我希望将图例放置(锁定)在“绘图区域”的左上角。由于多种原因,使用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")

enter image description here

干杯

4 个答案:

答案 0 :(得分:65)

更新:opts已被弃用。请改为使用theme,如in this answer.

所述

只是为了扩展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)

enter image description here

答案 1 :(得分:49)

更新:opts已被弃用。请改为使用theme,如in this answer.

所述

默认情况下,指南的位置基于绘图区域(即,由灰色填充的区域),但对齐是居中的。 所以你需要设置左上角对齐:

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

答案 2 :(得分:49)

我一直在寻找类似的答案。但发现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)

此代码将给出完全相似的情节。

答案 3 :(得分:12)

要扩展上述优秀答案,如果您想在图例和框外添加填充,请使用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。

相关问题