rggplot2plotpatchwork

Add single combined x-axis title to 2 patchwork plots


I have combined 2 separate ggplot2 plots using the patchwork package. How can I add a single combined x-axis title (e.g., "difference in SD units") to the patchwork plot below?

    dat_F <- structure(list(
  term = c("ExpA", "ExpA", "ExpB", "ExpB"), 
  estimate = c(-3.802316239, -5.885048428, -3.678601513, -4.103813546), 
  lci = c(-4.42722285, -6.476932582, -4.332540471, -4.751382827), 
  uci = c(-3.177409628, -5.293164273, -3.024662556, -3.456244265), 
  out = c("Out1", "Out2", "Out1", "Out2")), 
  class = "data.frame", row.names = c(NA, -4L))

    dat_M <- structure(list(
  term = c("ExpA", "ExpA", "ExpB", "ExpB"), 
  estimate = c(-1.134138758, -2.232236452, -0.935606149, -1.497766819), 
  lci = c(-1.841890314, -2.894980123, -1.662179615, -2.180915403), 
  uci = c(-0.426387201, -1.569492781, -0.209032682, -0.814618236), 
  out = c("Out3", "Out4", "Out3", "Out4")), 
  class = "data.frame", row.names = c(NA, -4L))

library(tidyverse)
library(patchwork)
library(RColorBrewer)

(plot_F <- ggplot(data = dat_F, aes(
  x = term, y = estimate, ymin = lci, ymax = uci)) + 
    theme_classic() + geom_pointrange(size = 0.3, aes(col = term)) + coord_flip() +
    ggtitle("a. Females") + geom_hline(yintercept = 0, lty = 1, size = 0.1, col = "red") + 
    facet_wrap(~ out, ncol = 1) + scale_color_brewer(palette = "Set2") +
    scale_y_continuous(limits = c(-7, 0.5), breaks=c(-6, -4, -2, 0)) + 
    ylab("")  + theme(axis.title.y = element_blank(), axis.ticks.y = element_blank(), 
      strip.background = element_blank(), axis.text.y = element_blank(), 
      legend.title = element_blank(), legend.position  = "bottom") +
    guides(colour = guide_legend(override.aes = list(size=0.8)))) 

(plot_M <- ggplot(data = dat_M, aes(
  x = term, y = estimate, ymin = lci, ymax = uci)) + 
    theme_classic() + geom_pointrange(size = 0.3, aes(col = term)) + coord_flip() +
    ggtitle("b. Males") + geom_hline(yintercept = 0, lty = 1, size = 0.1, col = "red") + 
    facet_wrap(~ out, ncol = 1) + scale_color_brewer(palette = "Set2") +
    scale_y_continuous(limits = c(-7, 0.5), breaks=c(-6, -4, -2, 0)) + 
    ylab("")  + theme(axis.title.y = element_blank(), axis.ticks.y = element_blank(), 
                      strip.background = element_blank(), axis.text.y = element_blank(), 
                      legend.title = element_blank(), legend.position  = "bottom") +
    guides(colour = guide_legend(override.aes = list(size=0.8)))) 

(combined_plot <- (plot_F + plot_M) + 
  plot_layout(guides = "collect") & theme(
    legend.position = 'bottom', 
    legend.direction = "horizontal",
    text = element_text(size = 8)))

enter image description here


Solution

  • By adding textGrob after the patchwork call you can add text as wanted.

    (combined_plot <- (plot_F + plot_M) + 
        plot_layout(guides = "collect") & theme(
          legend.position = 'bottom', 
          legend.direction = "horizontal",
          text = element_text(size = 8)))
    
    
    grid::grid.draw(grid::textGrob('diffence in SD units', x =.5, y=0.15))
    

    enter image description here