From 46db0bd5e44d5f97b5491aeda3936d14b626dbc2 Mon Sep 17 00:00:00 2001 From: Andreas Gammelgaard Damsbo Date: Fri, 8 Aug 2025 11:47:30 +0200 Subject: [PATCH] feat: even more arguments available when using wrap_plot_list --- R/data_plots.R | 39 ++++++++++++++++++++++++--------------- 1 file changed, 24 insertions(+), 15 deletions(-) diff --git a/R/data_plots.R b/R/data_plots.R index 6347ff0..f5b0def 100644 --- a/R/data_plots.R +++ b/R/data_plots.R @@ -738,7 +738,10 @@ line_break <- function(data, lineLength = 20, force = FALSE) { #' @param data list of ggplot2 objects #' @param tag_levels passed to patchwork::plot_annotation if given. Default is NULL #' @param title panel title -#' @param ... ignored for argument overflow +#' @param guides passed to patchwork::wrap_plots() +#' @param axes passed to patchwork::wrap_plots() +#' @param axis_titles passed to patchwork::wrap_plots() +#' @param ... passed to patchwork::wrap_plots() #' #' @returns list of ggplot2 objects #' @export @@ -747,6 +750,9 @@ wrap_plot_list <- function(data, tag_levels = NULL, title = NULL, axis.font.family = NULL, + guides = "collect", + axes = "collect", + axis_titles = "collect", ...) { if (ggplot2::is_ggplot(data[[1]])) { if (length(data) > 1) { @@ -762,9 +768,9 @@ wrap_plot_list <- function(data, })() |> align_axes() |> patchwork::wrap_plots( - guides = "collect", - axes = "collect", - axis_titles = "collect", + guides = guides, + axes = axes, + axis_titles = axis_titles, ... ) if (!is.null(tag_levels)) { @@ -784,13 +790,17 @@ wrap_plot_list <- function(data, cli::cli_abort("Can only wrap lists of {.cls ggplot} objects") } - if (inherits(x = out, what = "patchwork")) { - out & - ggplot2::theme(axis.text = ggplot2::element_text(family = axis.font.family)) - } else { - out + - ggplot2::theme(axis.text = ggplot2::element_text(family = axis.font.family)) + if (!is.null(axis.font.family)) { + if (inherits(x = out, what = "patchwork")) { + out <- out & + ggplot2::theme(axis.text = ggplot2::element_text(family = axis.font.family)) + } else { + out <- out + + ggplot2::theme(axis.text = ggplot2::element_text(family = axis.font.family)) + } } + + out } @@ -801,7 +811,7 @@ wrap_plot_list <- function(data, #' @returns list of ggplot2 objects #' @export #' -align_axes <- function(...,x.axis=TRUE,y.axis=TRUE) { +align_axes <- function(..., x.axis = TRUE, y.axis = TRUE) { # https://stackoverflow.com/questions/62818776/get-axis-limits-from-ggplot-object # https://github.com/thomasp85/patchwork/blob/main/R/plot_multipage.R#L150 if (ggplot2::is_ggplot(..1)) { @@ -819,13 +829,12 @@ align_axes <- function(...,x.axis=TRUE,y.axis=TRUE) { xr <- clean_common_axis(p, "x") suppressWarnings({ - p |> - purrr::map(p, \(.x){ + purrr::map(p, \(.x){ out <- .x - if (isTRUE(x.axis)){ + if (isTRUE(x.axis)) { out <- out + ggplot2::xlim(xr) } - if (isTRUE(y.axis)){ + if (isTRUE(y.axis)) { out <- out + ggplot2::ylim(yr) } out