【问题标题】:ggplot2 shared legend at the topggplot2 在顶部共享图例
【发布时间】:2017-05-30 14:36:32
【问题描述】:

我正在努力在两个 ggplot 图之间分享共同的图例,我使用arrange.grid 安排了这些图例。我得到的最接近的是:

Plot a legend and well-spaced universal y-axis and main titles in grid.arrange 但是使用这里的想法我只能在底部或顶部得到常见的图例。这是我的尝试:

library(ggplot2)
p1<-ggplot(data=diamonds)+geom_bar(aes(x=cut,fill=color))+scale_x_discrete("")+ylab("")+ggtitle("Title 1")
p2<-ggplot(data=diamonds)+geom_bar(aes(x=cut,fill=color))+scale_x_discrete("")+ylab("")+ggtitle("Title 2")
legend = gtable_filter(ggplotGrob(p1), "guide-box") 
grid.arrange(arrangeGrob(p1 + theme(legend.position="none"), 
                         p2 + theme(legend.position="none"),
                         nrow = 1,
                         top = textGrob("Main Title", vjust = -6, gp = gpar(fontface = "bold", cex = 1.5)),
                         left = textGrob("Global Y-axis Label", rot = 90, vjust = 2.5),
                         bottom = textGrob("Global X-axis Label", vjust =-1)),
             legend,
             nrow=1)

此外,本教程仅显示如何放置在底部或侧面:

https://cran.r-project.org/web/packages/cowplot/vignettes/shared_legends.html

使用此解决方案会有所帮助,但图表看起来仍然很丑。

p1<-ggplot(data=diamonds)+geom_bar(aes(x=cut,fill=color))+scale_x_discrete("")+ylab("")+ggtitle("Title 1")+guides(fill = guide_legend(title.position = "top",nrow=1))+theme(legend.position = "top", plot.title=element_text(hjust = 0,vjust=-1))
p2<-ggplot(data=diamonds)+geom_bar(aes(x=cut,fill=color))+scale_x_discrete("")+ylab("")+ggtitle("Title 2")

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

mylegend<-g_legend(p1)

grid.arrange(mylegend,nrow=2,heights=c(0.2,1),
             arrangeGrob(p1+ theme(legend.position="none"),
                         p2+ theme(legend.position="none"),
                        top = textGrob("Main Title", vjust =-6, gp = gpar(fontface = "bold", cex = 1.5)),
                        left = textGrob("Global Y-axis Label", rot = 90, vjust = 2.5),
                        bottom = textGrob("Global X-axis Label", vjust =-1),
                        nrow=1))

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    你可以使用这个功能

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

    然后您可以执行以下操作

    plot1 <- ggplot(...) + ... 
    
    mylegend<-g_legend(plot1)
    
    plot2 <- ggplot(...) + ... +  theme(legend.position="none")
    
    
    
      grid.arrange(arrangeGrob(plot1+ theme(legend.position="none")
                              ,plot2
                              nrow=1),
              mylegend,nrow=2,heights=c(10,1))
    

    更新

    要将图例放在首位,试试这个

      grid.arrange(mylegend,nrow=2,heights=c(0.05,1),
                   arrangeGrob( plot1+ theme(legend.position="none")
                               ,plot2
                               ,nrow=1)
                   )
    

    您可能需要对heights=c(0.05,1) 中的0.05 进行一些试验

    【讨论】:

    • 这个图例会出现在联合图的底部而不是顶部
    • 你有没有尝试在你的第一个情节中放置legend.position = "top"
    • 我添加了您的解决方案,但它仍然没有帮助,因为现在图例在我的主标题上方
    • 也许可以尝试做同样的事情,但是然后使用 R Markdown ?因为您可以在页面上放置标题,然后添加下面的图表,在您的情况下,这将是与以前相同的图表,但没有标题。否则我看不到任何其他方法可以通过它
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-03-29
    • 1970-01-01
    • 1970-01-01
    • 2017-01-23
    • 1970-01-01
    • 2023-03-15
    相关资源
    最近更新 更多