From d2b109e8c4c76c8c13fabcaa95fae540f8ce2d73 Mon Sep 17 00:00:00 2001 From: Teun van den Brand Date: Fri, 5 Jan 2024 13:22:01 +0100 Subject: [PATCH 1/4] grab multiple legends --- R/ggmatrix_legend.R | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/R/ggmatrix_legend.R b/R/ggmatrix_legend.R index d6de9e7fa..dad01430d 100644 --- a/R/ggmatrix_legend.R +++ b/R/ggmatrix_legend.R @@ -34,8 +34,11 @@ grab_legend <- function(p) { get_legend_from_gtable <- function(pTable) { ret <- ggplot2::zeroGrob() if (inherits(pTable, "gtable")) { - if ("guide-box" %in% pTable$layout$name) { + if (any(grepl("guide-box", pTable$layout$name))) { ret <- gtable_filter(pTable, "guide-box") + keep <- !vapply(ret$grobs, inherits, what = "zeroGrob", logical(1)) + keep <- paste0(ret$layout$name[keep], collapse = "|") + ret <- gtable_filter(ret, keep) } } class(ret) <- c("legend_guide_box", class(ret)) From a2663dbbddabb14437bb020a8e038090273ffe79 Mon Sep 17 00:00:00 2001 From: Teun van den Brand Date: Fri, 5 Jan 2024 13:22:29 +0100 Subject: [PATCH 2/4] `ggmatrix_gtable()` can deal with new legend structure --- R/ggmatrix_gtable.R | 36 ++++++++++++++++++++++++++---------- 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/R/ggmatrix_gtable.R b/R/ggmatrix_gtable.R index 47316e7c7..c63631a27 100644 --- a/R/ggmatrix_gtable.R +++ b/R/ggmatrix_gtable.R @@ -135,18 +135,34 @@ ggmatrix_gtable <- function( legend_obj <- legend } - legend_layout <- (pmg_layout[pmg_layout_name == "guide-box", ])[1, ] + legend_layout <- pmg_layout[grepl("guide-box", pmg_layout_name), ] class(legend_obj) <- setdiff(class(legend_obj), "legend_guide_box") - pmg$grobs[[legend_layout$grob_pos]] <- legend_obj - - legend_position <- ifnull(pm_fake$theme$legend.position, "right") - - if (legend_position %in% c("right", "left")) { - pmg$widths[[legend_layout$l]] <- legend_obj$widths[1] - } else if (legend_position %in% c("top", "bottom")) { - pmg$heights[[legend_layout$t]] <- legend_obj$heights[1] + index <- legend_layout$grob_pos[match(legend_obj$layout$name, legend_layout$name)] + pmg$grobs[index] <- legend_obj$grobs + + if ("guide-box" %in% legend_layout$name) { + legend_position <- ifnull(pm_fake$theme$legend.position, "right") + + if (legend_position %in% c("right", "left")) { + pmg$widths[[legend_layout$l]] <- legend_obj$widths[1] + } else if (legend_position %in% c("top", "bottom")) { + pmg$heights[[legend_layout$t]] <- legend_obj$heights[1] + } else { + stop(paste("ggmatrix does not know how display a legend when legend.position with value: '", legend_position, "'. Valid values: c('right', 'left', 'bottom', 'top')", sep = "")) # nolint + } } else { - stop(paste("ggmatrix does not know how display a legend when legend.position with value: '", legend_position, "'. Valid values: c('right', 'left', 'bottom', 'top')", sep = "")) # nolint + # From ggplot 3.5.0 onwards, a plot can have multiple legends + lr <- intersect(c("guide-box-left", "guide-box-right"), legend_obj$layout$name) + if (length(lr) > 0) { + width <- legend_obj$widths[legend_obj$layout$l[match(lr, legend_obj$layout$name)]] + pmg$widths[legend_layout$l[match(lr, legend_layout$name)]] <- width + } + + tb <- intersect(c("guide-box-bottom", "guide-box-right"), legend_obj$layout$name) + if (length(tb) > 0) { + height <- legend_obj$heights[legend_obj$layout$t[match(tb, legend_obj$layout$name)]] + pmg$heights[legend_layout$t[match(tb, legend_layout$name)]] <- height + } } } From 51d9f8c0c189e858baa61b513097d539a0782fdc Mon Sep 17 00:00:00 2001 From: Teun van den Brand Date: Fri, 5 Jan 2024 13:22:38 +0100 Subject: [PATCH 3/4] add news bullet --- NEWS.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/NEWS.md b/NEWS.md index 39bf82947..7d79a39f2 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,7 @@ # GGally (development version) +* Fix compatibility with ggplot2 3.5.0 (#481) + # GGally 2.2.0 ### Bug fixes From c79136637fffae078bffd779ee15ba9670115a08 Mon Sep 17 00:00:00 2001 From: Barret Schloerke Date: Thu, 8 Feb 2024 11:43:55 -0500 Subject: [PATCH 4/4] Add author --- NEWS.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/NEWS.md b/NEWS.md index 7d79a39f2..aaadf123d 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,6 +1,6 @@ # GGally (development version) -* Fix compatibility with ggplot2 3.5.0 (#481) +* Fix compatibility with ggplot2 3.5.0 (@teunbrand, #481) # GGally 2.2.0