-
Notifications
You must be signed in to change notification settings - Fork 9
Open
Description
Hi Teun,
when updating some older answers on SO to account for legendry (e.g. this one) I came across an issue where merging of guide_legend_groups fails and raises an error. Here is a minimal reprex of the issue:
library(ggplot2)
library(legendry)
mtcars2 <- mtcars
mtcars2$cyl_am <- paste0(mtcars$cyl, ".", mtcars$am)
ggplot(mtcars2, aes(mpg, hp)) +
geom_point(
aes(
shape = cyl_am, color = cyl_am
)
) +
guides(
color = "legend_group",
shape = "legend_group"
)
#> New names:
#> • `.group` -> `.group...4`
#> • `.group` -> `.group...6`
#> Error in `vec_slice()`:
#> ! `x` must be a vector, not `NULL`.Created on 2025-12-28 with reprex v2.1.1
Session info
sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#> setting value
#> version R version 4.5.2 (2025-10-31)
#> os macOS Sequoia 15.6.1
#> system aarch64, darwin20
#> ui X11
#> language (EN)
#> collate en_US.UTF-8
#> ctype en_US.UTF-8
#> tz Europe/Berlin
#> date 2025-12-28
#> pandoc 3.6.3 @ /Applications/RStudio.app/Contents/Resources/app/quarto/bin/tools/aarch64/ (via rmarkdown)
#> quarto 1.8.24 @ /Applications/quarto/bin/quarto
#>
#> ─ Packages ───────────────────────────────────────────────────────────────────
#> package * version date (UTC) lib source
#> cli 3.6.5 2025-04-23 [1] CRAN (R 4.5.0)
#> digest 0.6.39 2025-11-19 [1] CRAN (R 4.5.2)
#> dplyr 1.1.4 2023-11-17 [1] CRAN (R 4.5.0)
#> evaluate 1.0.5 2025-08-27 [1] CRAN (R 4.5.0)
#> farver 2.1.2 2024-05-13 [1] CRAN (R 4.5.0)
#> fastmap 1.2.0 2024-05-15 [1] CRAN (R 4.5.0)
#> fs 1.6.6 2025-04-12 [1] CRAN (R 4.5.0)
#> generics 0.1.4 2025-05-09 [1] CRAN (R 4.5.0)
#> ggplot2 * 4.0.1 2025-11-14 [1] CRAN (R 4.5.2)
#> glue 1.8.0 2024-09-30 [1] CRAN (R 4.5.0)
#> gtable 0.3.6 2024-10-25 [1] CRAN (R 4.5.0)
#> htmltools 0.5.9 2025-12-04 [1] CRAN (R 4.5.2)
#> knitr 1.51 2025-12-20 [1] CRAN (R 4.5.2)
#> labeling 0.4.3 2023-08-29 [1] CRAN (R 4.5.0)
#> legendry * 0.2.4 2025-09-14 [1] CRAN (R 4.5.0)
#> lifecycle 1.0.4 2023-11-07 [1] CRAN (R 4.5.0)
#> magrittr 2.0.4 2025-09-12 [1] CRAN (R 4.5.0)
#> otel 0.2.0 2025-08-29 [1] CRAN (R 4.5.0)
#> pillar 1.11.1 2025-09-17 [1] CRAN (R 4.5.0)
#> pkgconfig 2.0.3 2019-09-22 [1] CRAN (R 4.5.0)
#> R6 2.6.1 2025-02-15 [1] CRAN (R 4.5.0)
#> RColorBrewer 1.1-3 2022-04-03 [1] CRAN (R 4.5.0)
#> reprex 2.1.1 2024-07-06 [1] CRAN (R 4.5.0)
#> rlang 1.1.6 2025-04-11 [1] CRAN (R 4.5.0)
#> rmarkdown 2.30 2025-09-28 [1] CRAN (R 4.5.0)
#> rstudioapi 0.17.1 2024-10-22 [1] CRAN (R 4.5.0)
#> S7 0.2.1 2025-11-14 [1] CRAN (R 4.5.2)
#> scales 1.4.0 2025-04-24 [1] CRAN (R 4.5.0)
#> sessioninfo 1.2.3 2025-02-05 [1] CRAN (R 4.5.0)
#> tibble 3.3.0 2025-06-08 [1] CRAN (R 4.5.0)
#> tidyselect 1.2.1 2024-03-11 [1] CRAN (R 4.5.0)
#> vctrs 0.6.5 2023-12-01 [1] CRAN (R 4.5.0)
#> withr 3.0.2 2024-10-28 [1] CRAN (R 4.5.0)
#> xfun 0.55 2025-12-16 [1] CRAN (R 4.5.2)
#> yaml 2.3.12 2025-12-10 [1] CRAN (R 4.5.2)
#>
#> [1] /Users/stefan.moog/Library/R/arm64/4.5/library
#> [2] /Library/Frameworks/R.framework/Versions/4.5-arm64/Resources/library
#> * ── Packages attached to the search path.
#>
#> ──────────────────────────────────────────────────────────────────────────────On closer inspection, I found that the issue can be traced back to the merge method of GuideLegend, which does not account for the .group column. A possible fix would therefore be to add a custom merge method that accounts for the .group column, e.g. something like:
library(ggplot2)
library(legendry)
library(vctrs)
guide_legend_group2 <- function(
key = "group_split",
title = waiver(),
override.aes = list(),
nrow = NULL,
ncol = NULL,
theme = NULL,
position = NULL,
direction = NULL,
order = 0
) {
legendry:::check_position(position, theta = FALSE, inside = TRUE, allow_null = TRUE)
legendry:::check_argmatch(direction, c("horizontal", "vertical"), allow_null = TRUE)
legendry:::check_number_whole(nrow, min = 1, allow_null = TRUE)
legendry:::check_number_whole(ncol, min = 1, allow_null = TRUE)
legendry:::check_exclusive(nrow, ncol)
new_guide(
key = key,
title = title,
theme = theme,
override.aes = legendry:::rename_aes(override.aes),
nrow = nrow,
ncol = ncol,
order = order,
available_aes = "any",
name = "legend_group2",
direction = direction,
position = position,
super = GuideLegendGroup2
)
}
GuideLegendGroup2 <- ggproto(
"GuideLegendGroup2", GuideLegendGroup,
merge = function(self, params, new_guide, new_params) {
# Combine keys
#### Drop the .group column
new_params$key$.group <- new_params$key$.label <- new_params$key$.value <- NULL
####
params$key <- vec_cbind(params$key, new_params$key)
# Combine override.aes
params$override.aes <- c(params$override.aes, new_params$override.aes)
nms <- names(params$override.aes)
if (anyDuplicated(nms)) {
cli::cli_warn("Duplicated {.arg override.aes} is ignored.")
}
params$override.aes <- params$override.aes[!duplicated(nms)]
params$aesthetic <- union(params$aesthetic, new_params$aesthetic)
list(guide = self, params = params)
}
)
mtcars2 <- mtcars
mtcars2$cyl_am <- paste0(mtcars$cyl, ".", mtcars$am)
ggplot(mtcars2, aes(mpg, hp)) +
geom_point(
aes(
shape = cyl_am, color = cyl_am
)
) +
guides(
color = "legend_group2",
shape = "legend_group2"
)Created on 2025-12-28 with reprex v2.1.1
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels
