2012-05-25 77 views
43

问题在这里有点显而易见。我希望将这个图例置于(锁定)在“绘图区域”的左上角。出于多种原因,使用c(0.1,0.13)等不是一种选择。图例放置,ggplot,相对于绘图区域

有没有办法改变坐标的参考点,以便它们相对于绘图区域?

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

干杯

+0

你是什么意思的“阴谋地区”?该区域由灰色填充? – kohske

+0

@kohske是的,OP基本上希望能够确定'legend.position'的正确坐标,以将图例放置在“数据区域”或灰色区域的一角。正如我在聊天中提到的那样,我怀疑像你这样的人将不得不使用网格解决方案。 – joran

+0

@joran然后,这是默认行为,所有你需要的是设置正确的理由。 – kohske

回答

42

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

+0

谢谢,它不是'硬顶左',但从两个'边'等距离将做:) – nzcoops

+13

选择已弃用。改用主题。 – papirrin

+0

这会产生一个错误:'找不到函数'opts“'。 –

56

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

33

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

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

4

要在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))) 

这适用于最新版本的ggplot2这是V2。 2.1在撰写本文时。