aligning distinct non-facet plots in ggplot2 using Rpy2 in Python

前端 未结 3 1468
广开言路
广开言路 2021-01-02 01:55

I am combining two distinct plots into a grid layout with grid as suggested by @lgautier in rpy2 using python. The top plot is a density and and the bottom a ba

3条回答
  •  悲&欢浪女
    2021-01-02 02:20

    Split the legends from the plots (see ggplot separate legend and plot) , then use grid.arrange

    library(gridExtra)
    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]]
         legend
     }
     legend1 <- g_legend(p1)
     legend2 <- g_legend(p2)
    
    grid.arrange(p1 + theme(legend.position = 'none'), legend1, 
                 p2 + theme(legend.position = 'none'), legend2,
                ncol=2, widths = c(5/6,1/6))
    

    This is obviously the R implementation.

提交回复
热议问题