71

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

在此处输入图像描述

干杯

4

4 回答 4

73

更新: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)

在此处输入图像描述

于 2012-05-25T02:04:43.557 回答
69

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

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

于 2016-02-04T06:06:07.423 回答
56

更新: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")

在此处输入图像描述

如果要将指南放置在整个设备区域,可以调整 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)

在此处输入图像描述

于 2012-05-25T01:35:49.887 回答
21

要扩展上面的优秀答案,如果要在图例和框外部之间添加填充,请使用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)))

这适用于撰写本文时的最新版本ggplot2v2.2.1。

于 2017-06-11T16:19:28.477 回答