Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

plot_layout(axis_title = "collect") in combination with plot_spacer() #397

Open
nickhir opened this issue Sep 25, 2024 · 4 comments
Open

Comments

@nickhir
Copy link

nickhir commented Sep 25, 2024

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

@Mashin6
Copy link

Mashin6 commented Oct 1, 2024

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

@nickhir
Copy link
Author

nickhir commented Oct 1, 2024

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

image

@Mashin6
Copy link

Mashin6 commented Oct 2, 2024

This was run on patchwork_1.2.0

@nickhir
Copy link
Author

nickhir commented Oct 3, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants