I have the following code:
for(arch in c("Chronic decline", "Partial recovery", "Full recovery", "Resilient")) {
arch_data <- ntl_data %>% filter(archetype == arch)
if(nrow(arch_data) == 0) next
city_sectors <- arch_data %>%
distinct(city, sector) %>%
arrange(sector, city)
arch_plots <- list()
for(i in 1:nrow(city_sectors)) {
city_name <- city_sectors$city[i]
sector_name <- city_sectors$sector[i]
plot_data <- arch_data %>%
filter(city == city_name, sector == sector_name)
p <- ggplot(plot_data, aes(x = Date, y = ntl_scaled)) +
geom_rect(aes(xmin = start_date, xmax = end_date, ymin = -Inf, ymax = Inf),
fill = "grey80", alpha = 0.3, inherit.aes = FALSE) +
geom_line(color = sector_colors[sector_name], linewidth = 0.8) +
annotate("text", x = ymd("2018-01-01"), y = 0.95, label = city_name,
hjust = 0, vjust = 1, size = 3) +
scale_y_continuous(breaks = c(0, 0.5, 1), limits = c(0, 1)) +
scale_x_date(breaks = ymd(c("2018-01-01", "2021-01-01", "2023-01-01")),
date_labels = "%Y") +
theme_minimal() +
theme(
text = element_text(size = 10.5),
panel.grid = element_blank(),
panel.background = element_rect(fill = "white", color = NA),
panel.border = element_rect(fill = NA, color = archetype_colors[arch], linewidth = 1.5),
axis.line = element_line(color = archetype_colors[arch], linewidth = 1.5),
axis.text = element_text(size = 10.5, color = "black"),
axis.title = element_blank(),
plot.margin = margin(5, 5, 5, 5)
)
arch_plots[[i]] <- p
}
plot_list[[arch]] <- wrap_plots(arch_plots, ncol = 6)
}
final_plot <- wrap_plots(plot_list, ncol = 1)
n_total <- sum(sapply(plot_list, function(x) length(x$patches$plots)))
ggsave("C:/Users/nikos/OneDrive/Desktop/all_cities_sectors.pdf", final_plot,
width = 12, height = ceiling(n_total/6) * 2.5, device = cairo_pdf)
The resulting facets have different dimensions, why? How can I force them to have the same dimensions?
> sessionInfo()
R version 4.5.2 (2025-10-31 ucrt)
Platform: x86_64-w64-mingw32/x64
Running under: Windows 11 x64 (build 26200)
Matrix products: default
LAPACK version 3.12.1
locale:
[1] LC_COLLATE=English_United States.utf8 LC_CTYPE=English_United States.utf8 LC_MONETARY=English_United States.utf8
[4] LC_NUMERIC=C LC_TIME=English_United States.utf8
time zone: Europe/Budapest
tzcode source: internal
attached base packages:
[1] grid stats graphics grDevices utils datasets methods base
other attached packages:
[1] ggh4x_0.3.1 patchwork_1.3.2 gtable_0.3.6 lubridate_1.9.4 forcats_1.0.1 stringr_1.6.0 purrr_1.2.0 readr_2.1.6
[9] tidyr_1.3.2 tibble_3.3.0 ggplot2_4.0.1 tidyverse_2.0.0 dplyr_1.1.4
loaded via a namespace (and not attached):
[1] compiler_4.5.2 tidyselect_1.2.1 dichromat_2.0-0.1 systemfonts_1.3.1 scales_1.4.0 textshaping_1.0.4 R6_2.6.1
[8] labeling_0.4.3 generics_0.1.4 pillar_1.11.1 RColorBrewer_1.1-3 tzdb_0.5.0 rlang_1.1.6 utf8_1.2.6
[15] stringi_1.8.7 S7_0.2.1 timechange_0.3.0 cli_3.6.5 withr_3.0.2 magrittr_2.0.4 rstudioapi_0.17.1
[22] hms_1.1.4 lifecycle_1.0.4 vctrs_0.6.5 glue_1.8.0 farver_2.1.2 ragg_1.5.0 tools_4.5.2
[29] pkgconfig_2.0.3
If needed, I can create a dummy dataset. I couldn't share the one I am using because it's way too large.
