rggplot2plotpatchwork

Issue combining plots with patchwork in R: Inconsistent spacing between panels in two-level patched plots


I'm trying to combine several plots using patchwork in R, but I'm encountering an issue with misalignment and inconsistent spacing between the panels. The first set of plots is drawn with double spacing, while the other sets are drawn with single spacing. Below is my code and an image illustrating the problem.

library(ggplot2)
library(patchwork)

# Example data
mpg

# First set of plots
p1 <- ggplot(mpg, aes(class, cyl, group = manufacturer)) +
  geom_bar(stat = "identity", position = position_dodge())
p2 <- ggplot(mpg, aes(class, cyl, group = manufacturer)) +
  geom_line(aes(linetype = manufacturer, color = manufacturer), position = position_dodge2(width = 0.2), linewidth = 0.5)

pA <- p1 | p2
pA <- pA + plot_layout(axis_titles = 'collect')

# Second set of plots
p3 <- ggplot(mpg, aes(class, displ, group = manufacturer)) +
  geom_bar(stat = "identity", position = position_dodge())
p4 <- ggplot(mpg, aes(class, displ, group = manufacturer)) +
  geom_line(aes(linetype = manufacturer, color = manufacturer), position = position_dodge2(width = 0.2), linewidth = 0.5)

pB <- p3 | p4
pB <- pB + plot_layout(axis_titles = 'collect')

# Third set of plots
p5 <- ggplot(mpg, aes(class, year, group = manufacturer)) +
  geom_bar(stat = "identity", position = position_dodge())
p6 <- ggplot(mpg, aes(class, year, group = manufacturer)) +
  geom_line(aes(linetype = manufacturer, color = manufacturer), position = position_dodge2(width = 0.2), linewidth = 0.5)

pC <- p5 | p6
pC <- pC + plot_layout(axis_titles = 'collect')

# Combined final plot
pfail <- pA | pB | pC

pfail[[1]] <- pfail[[1]] + plot_layout(tag_level = 'new')
pfail[[2]] <- pfail[[2]] + plot_layout(tag_level = 'new')
pfail[[3]] <- pfail[[3]] + plot_layout(tag_level = 'new')

pfail <- pfail + plot_annotation(tag_levels = c('I', 'a'), tag_sep = '.', tag_suffix = ':') +
  plot_layout(guides = 'collect')

print(pfail)

This is an example with mpg data, look at the width of each one of the plots, it is supossed to be the same.

The first set of plots (pA) is drawn with double spacing between the panels, while the other sets of plots (pB and pC) are drawn with single spacing, causing a visual inconsistency in the final layout.

I have tried to add a space with plot_spacer()

But this leads to bigger problems when using more graphs, my dataset has about 20 graphs.

Does anyone know how I can fix this? Any help would be greatly appreciated!

Thanks in advance.


Solution

  • You might consider using wrap_plots instead of | and + to combine your plots. This will also fix the issue with the tag levels:

    library(ggplot2)
    library(patchwork)
    
    # Combined final plot
    pfail <- list(
      list(p1, p2), list(p3, p4), list(p5, p6)
    ) |>
      lapply(\(x) {
        x |> 
          wrap_plots() +
          plot_layout(
            axis_titles = "collect",
            tag_level = "new"
          )
      }) |>
      wrap_plots(nrow = 1)
    
    pfail <- pfail +
      plot_annotation(
        tag_levels = c("I", "a"), tag_sep = ".", tag_suffix = ":"
      ) +
      plot_layout(guides = "collect")
    
    pfail
    

    enter image description here