Search code examples
rggplot2patchwork

Move figure title using patchwork in ggplot


Using the example code and data below, I get a nice looking plot from patchwork. But, because patchwork leaves space above each individual plot in case you want to add individual plot-specific titles, this means the overall figure title is way too high up, with a lot of white space below it.

I haven't been able to find anything in the documentation about moving the title down, or otherwise eliminating that white space. Anyone have any tips?

Code

p1 <- dtest %>% 
  ggplot(., aes(x = score1, y = height1)) +
  geom_point(size = 4) +
  geom_smooth(method = "lm",
              size = 2,
              color = "firebrick") +
  labs(title = "",
       subtitle = "",
       x = "Score1",
       y = "Height1") +
  theme_minimal()

p2 <- dtest %>% 
  ggplot(., aes(x = score1, y = height1)) +
  geom_point(size = 4) +
  geom_smooth(method = "lm",
              size = 2,
              color = "firebrick") +
  labs(title = "",
       subtitle = "",
       x = "Score1",
       y = "Height2") +
  theme_minimal()

p3 <- dtest %>% 
  ggplot(., aes(x = score2, y = height2)) +
  geom_point(size = 4) +
  geom_smooth(method = "lm",
              size = 2,
              color = "firebrick") +
  labs(title = "",
       subtitle = "",
       x = "Score2",
       y = "") +
  theme_minimal()

p4 <- dtest %>% 
  ggplot(., aes(x = score2, y = height2)) +
  geom_point(size = 4) +
  geom_smooth(method = "lm",
              size = 2,
              color = "firebrick") +
  labs(title = "",
       subtitle = "",
       x = "Score2",
       y = "") +
  theme_minimal()

patchwork <- (p1 + p3) / (p2 + p4)
patchwork + plot_annotation(
  title = "Test Plot",
  subtitle = "Why is there so much white space under this"
)

Data

dtest <- structure(list(community = c("Winston", "Essex", "Sundry", "Fallston", 
"Springfield", "Lakeside", "Durham", "West Hill"), score1 = c(2, 
3, 1, 3, 5, 6, 2, 0), score2 = c(4, 7, 2, 4, 5, 8, 2, 5), height1 = c(1, 
2, 3, 5, 3, 8, 5, 3), height2 = c(7, 4, 2, 4, 4, 4, 2, 1)), row.names = c(NA, 
-8L), spec = structure(list(cols = list(community = structure(list(), class = c("collector_character", 
"collector")), score1 = structure(list(), class = c("collector_double", 
"collector")), score2 = structure(list(), class = c("collector_double", 
"collector")), height1 = structure(list(), class = c("collector_double", 
"collector")), height2 = structure(list(), class = c("collector_double", 
"collector"))), default = structure(list(), class = c("collector_guess", 
"collector")), delim = ","), class = "col_spec"), problems = <pointer: 0x00000233ec76fd90>, class = c("spec_tbl_df", 
"tbl_df", "tbl", "data.frame"))

Solution

  • Right now you've defined both title and subtitle as empty strings, but that doesn't actually get rid of the saved space for those plot elements. If you add + theme(plot.title = element_blank(), plot.subtitle = element_blank()) to the end of each ggplot(), that will remove the space saved for the empty string.