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
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.