patchwork icon indicating copy to clipboard operation
patchwork copied to clipboard

`plot_layout(axis_title = "collect")` in combination with `plot_spacer()`

Open nickhir opened this issue 1 year ago • 4 comments

Hi all,

I absolutely love the axis_title = "collect" feature. While using it, I have noticed that there is an (unintended?) interaction when used with plot_spacer(). Specifically, when adding a spacer to the patchwork, the row with the spacer will have a title... This is how to reproduce the "issue" :

# Load necessary libraries
library(ggplot2)
library(patchwork)
set.seed(123)

n <- 100  # Number of random points
data <- data.frame(
  x = rnorm(n),  # Random normal distribution for x-axis
  y = rnorm(n),  # Random normal distribution for y-axis
  category = sample(letters[1:3], n, replace = TRUE)  # Random categories
)

# First plot: Random scatter plot
plot1 <- ggplot(data, aes(x = x, y = y, color = category)) +
  geom_point(size = 3) +         # Scatter plot points
  theme_minimal() +              # Minimal theme for aesthetics
  labs(title = "Random Scatter Plot", 
       x = "Random X Values", 
       y = "Random Y Values", 
       color = "Category") +     # Labels for plot
  scale_color_manual(values = c("red", "blue", "green"))  # Custom colors

# Second plot: Random density plot of the 'x' variable
plot2 <- ggplot(data, aes(x = x, fill = category)) +
  geom_density(alpha = 0.5) +    # Density plot with transparency
  theme_minimal() +              # Minimal theme for aesthetics
  labs(title = "Density Plot of Random X Values", 
       x = "Random X Values", 
       y = "Density", 
       fill = "Category") +      # Labels for plot
  scale_fill_manual(values = c("red", "blue", "green"))  # Custom colors


plot_list <- list(plot1, plot1, plot1,
                  plot1, plot1, plot1,
                  plot1, plot1, plot1,
                  plot1, plot1, plot1,
                  plot1, plot1, plot1,
                  plot1, plot1, plot_spacer(),
                  plot2, plot2, plot2)

(wrap_plots(plot_list, ncol = 3)&theme(legend.position = "none")) + plot_layout(axis_titles = "collect")

This results in the following plot:

image

Any insights how to mitigate this are much appreciated! I am using version patchwork_1.3.0.9000

nickhir avatar Sep 25 '24 15:09 nickhir

This appears to not be limited to use of plot_spacer() but also to cases when plots don't fill full grid.

library(ggplot2)
library(patchwork)


d <- data.frame(x = rnorm(10, 1, 100),
                y = rnorm(20, 2, 100))
p <- NULL

for (i in 1:8) {
    p[[i]] <- d |> ggplot() + geom_point(aes(x = x, y = y))
}


wrap_plots(p) + plot_layout(axis_titles = 'collect')

image

Mashin6 avatar Oct 01 '24 17:10 Mashin6

Which version are you using @Mashin6? Your example works fine with patchwork_1.3.0.9000.

image

nickhir avatar Oct 01 '24 20:10 nickhir

This was run on patchwork_1.2.0

Mashin6 avatar Oct 02 '24 07:10 Mashin6

Well, I think in the latest version, the problem you describe is fixed.

nickhir avatar Oct 03 '24 09:10 nickhir