Home > Net >  How to decrease spacing between subplots
How to decrease spacing between subplots

Time:10-08

I have the following plot, I use ggplot2 and patchwork for a panel plot. Each row makes predictions about different target datasets so I thought it could be helpful to have a title for each row, which I just created with an empty ggplot annotate() Here is how it currently looks: enter image description here

Code for the Example

library(ggplot2)
library(patchwork)
p1 <- ggplot(mtcars)  
  geom_point(aes(mpg, disp))  
  ggtitle("Plot 1")

p2 <- ggplot(mtcars)  
  geom_boxplot(aes(gear, disp, group = gear))  
  ggtitle("Plot 2")

p3 <- ggplot(mtcars)  
  geom_point(aes(hp, wt, colour = mpg))  
  ggtitle("Plot 3")

p4 <- ggplot(mtcars)  
  geom_bar(aes(gear))  
  facet_wrap(~cyl)  
  ggtitle("Plot 4")

ggplot()  
  annotate(geom = "text", x = 1, y = 1, label = "Predictions on Dataset 1",size=8)  
  theme_void() -> title1
ggplot()  
  annotate(geom = "text", x = 1, y = 1, label = "Predictions on Dataset 2",size=8)  
  theme_void() -> title2


title1 /
  (p1 | p2) /
  title2 /
  (p3 | p4)  
  plot_annotation(
    tag_levels = list(c("","A.1) Model Name 1", "B.1) Model Name 2", "","A.2) Model Name 1", "B.2) Model Name 2")),
    theme = theme(plot.title = element_text(size = 24))
  ) &
  theme(

  )

Question
How can I reduce the space between the title1 and title2 and the other plots?

Desired Output: enter image description here

CodePudding user response:

title1 / (p1 | p2) /
  title2 / (p3 | p4)  
  plot_layout(heights = c(1,6,1,6))  
  ....

enter image description here

And you can adjust the text size in your title1 and title2 to make those more consistent with the other text. Here's with size = 5:

enter image description here

  • Related