patchwork icon indicating copy to clipboard operation
patchwork copied to clipboard

`plot_layout(axis_title = "collect")` does not work with facets.

Open phispu opened this issue 1 year ago • 5 comments

The axis_title = "collect" argument in plot_layout doesn't seem to work with facets. I'm not sure if this might be a bug, or if it was intended?

In example 1 below, plot 3 (p3, no facets) can have its axis titles collected.

In examples 2 (p3_facet_x) and 3 (p3_facet_y), the faceted side doesn't have its axis title collected.

data(mtcars)
# Create plots
p1 <- ggplot(mtcars) + 
  geom_point(aes(mpg, disp)) + 
  ggtitle('Plot 1') +
  labs(x = 'X Label', y = 'Y Label')
p2 <- ggplot(mtcars) + 
  geom_point(aes(disp, hp)) + 
  ggtitle('Plot 2') +
  labs(x = 'X Label', y = 'Y Label')
p3 <- ggplot(mtcars) + 
  geom_point(aes(drat, wt)) + 
  ggtitle('Plot 3a') +
  labs(x = 'X Label', y = 'Y Label')
  # facet plots
p3_facet_x <- ggplot(mtcars) + 
  geom_point(aes(drat, wt)) + 
  facet_grid(. ~ gear) + 
  ggtitle('Plot 3b') +
  labs(x = 'X Label', y = 'Y Label')
p3_facet_y <- ggplot(mtcars) + 
  geom_point(aes(drat, wt)) + 
  facet_grid(gear ~ .) + 
  ggtitle('Plot 3c') +
  labs(x = 'X Label', y = 'Y Label')
# Example 1: axis titles collect
p1 + p2 + p3 + 
  plot_layout(axis_titles = 'collect')
# Example 2: x-axis does not collect all the way
p1 + p2 + p3_facet_x +
  plot_layout(axis_titles = 'collect')
# Example 3: y-axis does not collect all the way
p1 + p2 + p3_facet_y +
  plot_layout(axis_titles = 'collect')

phispu avatar Sep 04 '24 20:09 phispu

Try with the dev version - this should be fixed

thomasp85 avatar Sep 09 '24 13:09 thomasp85

@thomasp85 I'm having exactly the same problem...

This is an example of one code block I'm using for plotting:

k2plot <-
  ggplot(tbl2, aes(factor(sampleID2), value, fill=comp)) +
  geom_col(width=1) +
  facet_grid(~fct_inorder(location2), switch="x", scales="free", space="free") +
  theme_minimal() + labs(x="Individuals", title="K=2", y="components") +
  scale_y_continuous(expand=c(0, 0)) +
  scale_x_discrete(expand=c(0, 0)) +
  theme(
    panel.spacing.x=unit(0.1, "lines"),
    axis.text.x=element_blank(),
    strip.text.x=element_text(angle=45),
    panel.border=element_rect(colour="black", fill=NA, linewidth=3)
  ) +
  scale_fill_brewer(palette="Paired", guide='none')

I'm repeating this 5 times and collecting all plots with the following:

((k2plot + k3plot + k4plot) / (k5plot + k6plot)) + plot_layout(axis_titles="collect")

However, the result is like in the image below:

Image

The intended output should have a single, centered label on the x- and y-axes indicating the Individuals and the components, respectively. Let me know, thanks!

Overcraft90 avatar Feb 20 '25 11:02 Overcraft90

I actually solved it using the design argument of patchwork. But is it always necessary in these cases when organizing facets on two rows?

Overcraft90 avatar Feb 20 '25 14:02 Overcraft90

@Overcraft90 , how exactly did you solve it with the design argument?

trizniak avatar Oct 22 '25 18:10 trizniak

@trizniak I used this code

##design solution
design <- "ABC
           DEF
           GHI"
k2plot + k3plot + k4plot + k5plot + k6plot + k7plot + k8plot + k9plot + k10plot + 
  plot_layout(axis_titles="collect", design=design)

if I remember correctly. Let me know!

Overcraft90 avatar Oct 23 '25 12:10 Overcraft90