【问题标题】:How can I move the legend position with grid.arrange如何使用 grid.arrange 移动图例位置
【发布时间】:2017-04-05 00:50:44
【问题描述】:

我试图在一页中安排 4 个图,将图例放在底部中心

我用它从其中一个地块中获取图例(因为它们对于四个地块来说是相同的)

    get_legend<-function(myggplot){
    tmp <- ggplot_gtable(ggplot_build(myggplot))
    leg <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box")
    legend <- tmp$grobs[[leg]]
    return(legend)
    }

然后,我从任何情节中得到图例

   legend <- get_legend(p2)

所以对于我使用的数字:

 tt <-grid.arrange(arrangeGrob(p6, p7, p8, p9, legend,
                          nrow = 2, #
                          left = textGrob("Mammalian species richness", rot = 90, vjust = 1, 
                                          gp = gpar(fontsize = 12))))

但我得到的是这样的:

如何将图例移动到中心按钮并有 2 行 2 列? 命令nrowncol 不起作用,因为我收到一条错误消息,也不是类似

tt  <- tt + theme(legend.position ="bottom"). 

ps。以防万一此信息很重要,如果我尝试不放置任何图例,我可以根据需要将四个图放在一个网格中,但没有图例,所以,你知道不适合出版。

【问题讨论】:

    标签: r ggplot2 data-visualization gridextra


    【解决方案1】:

    您可以在grid.arrange 中使用参数layout_matrix 进行更多控制。

    一个可重现的例子,

    library(ggplot2)
    library(gridExtra)
    
    get_legend<-function(myggplot){
        tmp <- ggplot_gtable(ggplot_build(myggplot))
        leg <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box")
        legend <- tmp$grobs[[leg]]
        return(legend)
        }
    
    p <- ggplot()
    p2 <- ggplot(mtcars, aes(mpg, wt, col=factor(am))) + geom_point()
    legend <- get_legend(p2)
    
    
    grid.arrange(p, p, p, p, legend, 
                 layout_matrix=rbind(c(1,2,3), c(4,5,5)))
    

    哪个产生

    也许你想要

    grid.arrange(p, p ,p, p, legend, 
                 layout_matrix=rbind(c(1,1,2,2,5), c(3,3,4,4,5)))
    

    【讨论】:

    • 非常感谢您的回复 user20650,尽管这不是一个正确的问题。它起作用了,我还使用 layout_matrix=rbind(c(1,2), c(3,4), c(5,5))) 将其放置在按钮上。但是,向右更好,因为按钮中有很多空间和图例仍然在两行中,并且不知道如何使其变平...谢谢!
    • 为了使其平整,将图例放置在原始ggplot的底部:即p2 = p2 + theme(legend.position="bottom"),然后像以前一样继续。
    • 非常感谢,它成功了。但是,我一直试图删除图例和情节之间的空间,但没有运气。你能否给我一些关于如何做到这一点的见解。由于这个空间,将图例放置在按钮上(水平或垂直)后,绘图会缩小。这不会发生在正确的位置。
    • @lep ;您可能可以通过在 layout_matrix 中添加额外的元素来做到这一点,就像我在第二个示例中所做的那样(请参阅 1 到 4 的重复值以增加这些图的宽度。但是,这可能更容易grid.arrange(arrangeGrob(p,p,p,p, nrow=2), legend, ncol=1, heights=c(9,1))跨度>
    • 扁平化图例后,您应该添加一个主题来删除图例,例如p = p + theme(legend.position = 'none')
    【解决方案2】:

    为了补充user20650的回答并完整回答OP,我已经绘制了四次相同的情节,但我解决了这个问题。

    为了能够使用以下代码,您需要安装cowplotggpubr 包以进行gridExtra 格式设置。

    虽然OP有获取图例信息的功能,但我遇到了一些问题,所以我不得不使用cowplot

    layout_matrix格式请参考this page

    library(cowplot)
    library(ggplot2)
    library(gridExtra)
    library(ggpubr)
    
    
    
    p =  ggplot2::ggplot(mtcars, aes(mpg, wt, col = factor(am))) + 
      ggplot2::geom_point() + 
      scale_color_discrete(name = "Car Type", labels=c("fast", "slow"), ) +
      labs(x = '', y = '') +
      ggtitle('Car')  +
      theme_bw() +
      theme(plot.title = element_text(size = 8, hjust = 0.5),
            axis.text.x = element_text(angle = 30, hjust = 1), 
            legend.position = 'top')
    legend = cowplot::get_legend(p)
    
    p = p + ggplot2::theme(legend.position = 'none')
    
    gridExtra::grid.arrange(grobs = list(p, p, p, p, legend),
                            left = ggpubr::text_grob("Width", 
                                                     rot = 90, 
                                                     vjust = 1),
                            bottom = ggpubr::text_grob("Mile Per Gallon", 
                                                       rot = 0, 
                                                       vjust = 0),
                            
                            layout_matrix = rbind(c(5, 5), 
                                                  c(1, 2), 
                                                  c(1, 2),
                                                  c(1, 2), 
                                                  c(1, 2),
                                                  c(1, 2), 
                                                  c(1, 2),
                                                  c(1, 2),
                                                  c(3, 4),
                                                  c(3, 4),
                                                  c(3, 4),
                                                  c(3, 4),
                                                  c(3, 4),
                                                  c(3, 4),
                                                  c(3, 4)))
    

    【讨论】:

    • 但不需要ggpubr; def 更容易使用grid 包和grid::textGrob
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-22
    • 1970-01-01
    • 2021-10-19
    • 1970-01-01
    • 2021-08-17
    • 2011-02-26
    相关资源
    最近更新 更多