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

nzc*_*ops 58 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")
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

干杯

nzc*_*ops 69

更新: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)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述


Kum*_*lam 53

我一直在寻找类似的答案.但发现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)
Run Code Online (Sandbox Code Playgroud)

此代码将给出完全相似的图.


koh*_*ske 51

更新: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")
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

如果要将指南放在整个设备区域,可以调整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)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

  • opts已被弃用.请改用主题. (16认同)

Con*_*ngo 12

要扩展上面的excellend答案,如果要在图例和框外添加填充,请使用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)))
Run Code Online (Sandbox Code Playgroud)

ggplot2在撰写本文时,其最新版本为v2.2.1.

  • 带边距的好扩展!如果你想让所有边距相同,只需要一个小小的建议,使用`rep(50,times = 4)`来更容易地调整边际值 (2认同)