Skip to content

Commit

Permalink
Compatibility with ggplot2 3.5.0 (#483)
Browse files Browse the repository at this point in the history
  • Loading branch information
teunbrand authored Feb 8, 2024
1 parent e19b6b2 commit 117c0ae
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 11 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# GGally (development version)

* Fix compatibility with ggplot2 3.5.0 (@teunbrand, #481)

# GGally 2.2.0

### Bug fixes
Expand Down
36 changes: 26 additions & 10 deletions R/ggmatrix_gtable.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}

Expand Down
5 changes: 4 additions & 1 deletion R/ggmatrix_legend.R
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down

0 comments on commit 117c0ae

Please sign in to comment.