-
Notifications
You must be signed in to change notification settings - Fork 167
Description
First of all - thank you for this great package!
I am using patchwork to create plots with axis breaks. However, in cases with plot titles, these unfortunately will be present in all subplots when defined in the original plot. Defining the title within plot.annotation() is not helpful as I need the title centered above the plot. So basically between xmin and xmax like when collecting the y-axis title and not centered based on the image area as exaggerated and demonstrated below.
Right now I am using the secondary top x-axis title to imitate the plot title, but the code gets messy real fast the more sub-panels I have.
Is there a way to introduce the option to also collect plot titles the same way collecting the y-axis titles works?
library(ggplot2)
library(patchwork)
df <- data.frame(x = 1:10, y = 1:10)
p <- ggplot(df, aes(x,y)) + geom_point(aes(color = y)) +
labs(title = "ggplot title", x = "Axis title", y = "Axis title") +
theme(axis.text.x.top = element_blank(), axis.ticks.x.top = element_blank(), axis.line.x.top = element_blank(), # Remove upper axis lines and text
axis.title.x.top = element_text(size = 14, margin = margin(b = 10))) # Imitate plot title theme
# Cut axes
p1 <- p + scale_x_continuous(limits = c(0,2.25), expand = FALSE, breaks = seq(0,10, by = 1),
sec.axis = dup_axis(name = "Sec. axis title")) # Add top x-axis to collect title
p2 <- p + scale_x_continuous(limits = c(7.75,10), expand = FALSE, breaks = seq(0,10, by = 1),
sec.axis = dup_axis(name = "Sec. axis title")) # Add top x-axis to collect title
# Assemble
p1 + p2 + plot_spacer() + plot_layout(axes = "collect", guides = "collect") +
plot_annotation(title = "Patchwork title", theme = theme(plot.title = element_text(hjust = 0.5)))
#> Warning: Removed 8 rows containing missing values or values outside the scale range
#> (`geom_point()`).
#> Warning: Removed 7 rows containing missing values or values outside the scale range
#> (`geom_point()`).
#> Warning: annotation$theme is not a valid theme.
#> Please use `theme()` to construct themes.Created on 2025-09-16 with reprex v2.1.1
