Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compatibility with ggplot2 3.5.0 #483

Merged
merged 4 commits into from
Feb 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
36 changes: 26 additions & 10 deletions R/ggmatrix_gtable.R
Original file line number Diff line number Diff line change
@@ -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
}
}
}

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