Skip to content

Commit

Permalink
Fix #367 and fix #369
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasp85 committed Jul 28, 2024
1 parent d33fd15 commit e4b0839
Showing 2 changed files with 10 additions and 3 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -3,6 +3,8 @@
* `free()` now better aligns plots in horizontal direction
* Plot backgrounds are now always placed beneath all other elements in the
patchwork (#370)
* Axis titles can now reliably be collected even with faceted plots (#367 and
#369)

# patchwork 1.2.0

11 changes: 8 additions & 3 deletions R/collect_axes.R
Original file line number Diff line number Diff line change
@@ -27,7 +27,7 @@ collect_axis_titles <- function(gt, dir = "x", merge = TRUE) {
layout[layout %in% patch_index] <- NA # Remove patches

# Mark duplicated grobs
structure <- grob_id(gt$grobs, layout, byrow = dir == "x", merge = merge)
structure <- grob_id(gt$grobs, layout, byrow = dir == "x", merge = merge, unpack = TRUE)

# If all title grobs are unique, there is nothing to collapse
if (anyDuplicated(structure[!is.na(structure)]) == 0) {
@@ -243,12 +243,17 @@ is_zero <- function(x) {

# Determine uniqueness of grobs
#' @importFrom stats ave
grob_id <- function(grobs, layout, byrow, merge = FALSE) {
grob_id <- function(grobs, layout, byrow, merge = FALSE, unpack = FALSE) {

# Hash the grobs to determine unique grobs
valid <- !is.na(layout)
idx <- as.vector(layout)[valid]
hash <- vapply(grobs[idx], function(x) hash(unname_grob(x)), character(1))
hash <- vapply(grobs[idx], function(x) {
if (unpack && inherits(x, "gtable") && length(x$grobs) == 1) {
x <- x$grobs[[1]]
}
hash(unname_grob(x))
}, character(1))

# For multi-cell grobs, compute an extra identifier
if (!merge) {

0 comments on commit e4b0839

Please sign in to comment.