Reduce column height in a multiple plot in ggplot2

Viewed 156

I can create 3 plots and then merge them within the same figure with the following code :

p1 <- qplot(mpg, wt, data=mtcars)
p2 <- p1
p3 <- p1 + theme(axis.text.y=element_blank(), axis.title.y=element_blank())


plot_grid(p1,p2,p3, align = "v",ncol=3)

Then I get :

enter image description here

And and wondered if someone knew if it was possible to reduce the column height ratio of each column independently within the multiplot ? and Get something like :

enter image description here

2 Answers

Making use of patchwork one option to achieve your desired result may look like so:

library(ggplot2)
library(patchwork)

p1 <- qplot(mpg, wt, data = mtcars)
p2 <- p1
p3 <- p1 + theme(axis.text.y = element_blank(), axis.title.y = element_blank())

pp1 <- p1
pp2 <- plot_spacer() / p2 + plot_layout(heights = c(1, 3))
pp3 <- plot_spacer() / p3 + plot_layout(heights = c(1, 1))

pp1 + pp2 + pp3

Another approach using patchwork:

library(ggplot2)
library(patchwork)


p1 <- qplot(mpg, wt, data=mtcars)
p2 <- p1
p3 <- p1 + theme(axis.text.y=element_blank(), axis.title.y=element_blank())

design <- 
  "1##
   12#
   123"

p1 + p2 + p3 + plot_layout(design = design)

Created on 2021-08-21 by the reprex package (v2.0.0)

Related