This code takes an alternative approach to the fancy patchwork hacking hacking Andrew Heiss used to get to shared x- and y-axis labels in a patchwork plot.
https://fediscience.org/@andrew/109558941216921694
library(ggplot2)
library(patchwork)
huron <- data.frame(year = 1875:1972, level = as.vector(LakeHuron))
h <- ggplot(huron, aes(year))
h1 <- h +
geom_ribbon(aes(ymin = level - 1, ymax = level + 1), fill = "grey70") +
geom_line(aes(y = level))
h2 <- h + geom_area(aes(y = level))
# Get a shared x-axis
h_patch <- h1 + h2 & xlab(NULL) & theme(plot.margin = margin(5.5, 5.5, 0, 5.5))
wrap_elements(panel = h_patch) +
labs(tag = "year") +
theme(
plot.tag = element_text(size = rel(1)),
plot.tag.position = "bottom"
)
# Get a shared y-axis
h_patch <- h1 / h2 & ylab(NULL) & theme(plot.margin = margin(5.5, 5.5, 5.5, 0))
wrap_elements(h_patch) +
labs(tag = "level") +
theme(
plot.tag = element_text(size = rel(1), angle = 90),
plot.tag.position = "left"
)
Created on 2022-12-22 with reprex v2.0.2