How to remove white spcaes around a bottom-aligned legend with cowplot

294 Views Asked by At

I am using cowplot to arrange two plots with a common legend below. The approach works, yet the legend has whitespace around I would like to get rid of.

Note: I am ware of the ggpubr::ggarange common.legend option I though want to stick to cowplot::plot_grid (which masks ggpubr::get_legend)

library(tidyverse)
library(ggpubr)
library(cowplot)
theme_set(theme_cowplot())

p1 <- iris %>% 
  ggplot(aes(x=Sepal.Width,y=Petal.Width,color=Species)) + 
  geom_point() + coord_fixed()


p2 <- iris %>% 
  ggplot(aes(x=Sepal.Width,y=Petal.Width,color=Species)) + 
  geom_point() + coord_fixed()



compound_plot <- plot_grid(p1 + theme(legend.position="none"),
                           p2 + theme(legend.position="none"),
                           ncol = 2,
                           labels = c("A","B"))

legend <- get_legend(p1 + 
                       theme(legend.direction = "horizontal",
                             legend.justification="center" ,
                             legend.box.just = "bottom"))

plot_grid(compound_plot,
          legend,
          ncol = 1)

enter image description here

Thanks in advance!

1

There are 1 best solutions below

1
On BEST ANSWER

You can set rel_heights inside plot_grid

plot_grid(compound_plot,
          legend,
          ncol = 1, rel_heights = c(0.95, 0.05))

enter image description here