首页 文章

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

提问于
浏览
49

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

  • 8

    Update: opts has been deprecated. Please use theme instead, as described 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

  • 44

    Update: opts has been deprecated. Please use theme instead, as described 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

  • 62

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

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

  • 42

    要扩展上面的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 .

相关问题