2

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

chippycentra
  • 3,396
  • 1
  • 6
  • 24

2 Answers2

2

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

stefan
  • 90,330
  • 6
  • 25
  • 51
  • can I modify the ratio myself ? for instance if I want that p2 is 3/4 of p1 ? – chippycentra Aug 21 '21 at 08:49
  • For sure. I just made an edit. What I would do is to first set up the plots for each column separately which also makes the code a bit cleaner. Thereby you could use plot_layout to set the relative heights. Afterwards you could glue the column plots together. – stefan Aug 21 '21 at 09:03
2

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)

Peter
  • 11,500
  • 5
  • 21
  • 31