From 32143363aff13090489897a5ae9126011bc9c74c Mon Sep 17 00:00:00 2001 From: Yunuuuu Date: Tue, 26 Aug 2025 20:49:40 +0800 Subject: [PATCH] fix: respect `theme(strip.placement)` when strip position is ambiguous --- R/plot_multipage.R | 2 +- R/plot_patchwork.R | 18 ++++++++++++------ R/wrap_ggplot_grob.R | 10 ++++++++-- 3 files changed, 21 insertions(+), 9 deletions(-) diff --git a/R/plot_multipage.R b/R/plot_multipage.R index f59b2df..3c57740 100644 --- a/R/plot_multipage.R +++ b/R/plot_multipage.R @@ -116,7 +116,7 @@ ggplot_build.fixed_dim_ggplot <- function(plot, ...) { ggplot_gtable.fixed_dim_build <- function(data) { dim <- data$plot$fixed_dimensions table <- NextMethod() - table <- add_strips(table) + table <- add_strips(table, data$plot$theme) table <- add_guides(table, FALSE) panel_pos <- find_panel(table) table$widths[seq_len(panel_pos$l - 1)] <- unit(dim$l, 'mm') diff --git a/R/plot_patchwork.R b/R/plot_patchwork.R index 1d4e180..80ca261 100644 --- a/R/plot_patchwork.R +++ b/R/plot_patchwork.R @@ -301,8 +301,10 @@ plot_table <- function(x, guides) { #' @importFrom ggplot2 ggplotGrob #' @export plot_table.ggplot <- function(x, guides) { - gt <- ggplotGrob(x) - gt <- add_strips(gt) + data <- ggplot_build(x) + gt <- ggplot_gtable(data) + # use the completed theme when adding strips + gt <- add_strips(gt, data$plot$theme) add_guides(gt, guides == 'collect') } #' @export @@ -939,10 +941,10 @@ set_border_sizes <- function(gt, l = NULL, r = NULL, t = NULL, b = NULL) { #' @importFrom gtable gtable_add_rows gtable_add_cols #' @importFrom grid unit #' @importFrom ggplot2 find_panel -add_strips <- function(gt) { +add_strips <- function(gt, theme = NULL) { panel_loc <- find_panel(gt) strip_pos <- switch( - find_strip_pos(gt), + find_strip_pos(gt, theme), inside = 0, outside = 2 ) @@ -1059,7 +1061,7 @@ add_guides <- function(gt, collect = FALSE) { } gt } -find_strip_pos <- function(gt) { +find_strip_pos <- function(gt, theme) { panel_loc <- find_panel(gt) ind <- grep('strip-t', gt$layout$name) if (length(ind) != 0 && panel_loc$t - min(gt$layout$t[ind]) != 1) { @@ -1077,7 +1079,11 @@ find_strip_pos <- function(gt) { if (length(ind) != 0 && panel_loc$l - min(gt$layout$l[ind]) != 1) { return('outside') } - 'inside' + if (is.null(theme)) { + 'inside' + } else { + calc_element("strip.placement", theme) %||% "inside" + } } set_panel_dimensions <- function(gt, panels, widths, heights, fixed_asp, design) { diff --git a/R/wrap_ggplot_grob.R b/R/wrap_ggplot_grob.R index 9ca94b8..5d73e24 100644 --- a/R/wrap_ggplot_grob.R +++ b/R/wrap_ggplot_grob.R @@ -46,13 +46,19 @@ wrap_ggplot_grob <- function(x) { attr(patch, 'table') <- x patch } + +#' @importFrom ggplot2 ggplot_build ggplot_gtable +#' @importFrom gtable gtable_add_grob #' @export patchGrob.table_patch <- function(x, guides = 'auto') { + data <- ggplot_build(x) gt <- attr(x, 'table') - gt <- add_strips(gt) + # use the completed theme when adding strips + gt <- add_strips(gt, data$plot$theme) gt <- add_guides(gt, guides == 'collect') if ("tag" %in% names(x$labels)) { - plot <- add_guides(add_strips(ggplotGrob(x))) + # use the completed theme when adding strips + plot <- add_guides(add_strips(ggplot_gtable(data), data$plot$theme)) tag_idx <- which(plot$layout$name == "tag") gt <- gtable_add_grob( gt,