patchwork icon indicating copy to clipboard operation
patchwork copied to clipboard

Axes labels are not properly placed when combining faceted and single plots

Open eggrandio opened this issue 10 months ago • 2 comments

I am combinining four plots, two of them have facets and two of them are single plots. They all have the same y axes.

When I arrange them, the y axes labels of the single plots are not placed properly, and they have extra padding:

Image

Here is the code I have used:

faceted_plot<-
  ggplot(input_data, aes(x = Line, y = indel, fill = gene)) +
  geom_bar(position = "dodge", stat = "identity") +
  scale_y_continuous(expand = expansion(mult = c(0, 0))) +
  scale_fill_manual(values = wes_palette("GrandBudapest1")[1:2])
  facet_grid(~Plasmid, 
             scales = "free_x", 
             space = "free", 
             switch = "x") +
  labs(y = "% indel",
       fill = "Gene") +
  theme(legend.justification = "left", strip.placement = "outside",
        strip.background = element_blank(),
        axis.title.x = element_blank())

single_plot <-
  ggplot(input_data2, aes(x = Line, y = indel, fill = gene)) +
  geom_bar(position = "dodge", stat = "identity") +
  scale_y_continuous(expand = expansion(mult = c(0, 0))) +
  scale_fill_manual(values = wes_palette("GrandBudapest1")) +
  labs(y = "% indel",
       fill = "Gene") +
  theme(legend.justification = "left")

final_plot <- faceted_plot1 / faceted_plot2 / single_plot1 / single_plot2

eggrandio avatar Mar 20 '25 14:03 eggrandio

Thanks for the reply. I removed the strip in the example for simplicity, but I need to have them as I use them as x axes labels. If there is no other way of doing it, I might have to assign the facet names to the x axes labels of the faceted plots.

eggrandio avatar Mar 20 '25 18:03 eggrandio

Aw. Sorry. My fault. One option to fix your issue would be to use a fake facet_grid for your single plots too:

library(wesanderson)
library(ggplot2)
library(patchwork)

set.seed(123)

input_data <- input_data2 <- data.frame(
  Line = sample(1:40, 100, replace = TRUE),
  gene = sample(letters[1:2], 100, replace = TRUE),
  Plasmid = sample(LETTERS[1:2], 100, replace = TRUE),
  indel = runif(100)
)

faceted_plot <-
  ggplot(input_data, aes(x = Line, y = indel, fill = gene)) +
  geom_bar(position = "dodge", stat = "identity") +
  scale_y_continuous(expand = expansion(mult = c(0, 0))) +
  scale_fill_manual(values = wes_palette("GrandBudapest1")[1:2]) +
  facet_grid(~Plasmid,
    scales = "free_x",
    space = "free_x",
    switch = "x"
  ) +
  labs(
    y = "% indel",
    fill = "Gene"
  ) +
  theme(
    legend.justification = "left",
    strip.background = element_blank(),
    strip.placement = "outside",
    axis.title.x = element_blank()
  )

single_plot <-
  ggplot(input_data2, aes(x = Line, y = indel, fill = gene)) +
  geom_bar(position = "dodge", stat = "identity") +
  scale_y_continuous(expand = expansion(mult = c(0, 0))) +
  scale_fill_manual(values = wes_palette("GrandBudapest1")) +
  facet_grid(~ factor(1),
    scales = "free_x",
    space = "free_x",
    switch = "x"
  ) +
  labs(
    y = "% indel",
    fill = "Gene"
  ) +
  theme(
    strip.placement = "outside",
    strip.text = element_blank()
  )

final_plot <- faceted_plot / single_plot

final_plot

trekonom avatar Mar 20 '25 19:03 trekonom