-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Closed
Description
I found a strange issue with using update_ggplot
through +
method.
I expected for it to be easy to add custom objects to any ggplot object with update_ggplot
method but it seems like when using the +
method, the latter object is not used?
Here is the code to reproduce the bug:
library(S7)
library(ggplot2)
# simple class to apply a function to a ggplot object
mod_gg <- new_class(
"mod_gg",
S7_object,
properties = list(
action = class_function
)
)
method(update_ggplot, list(mod_gg, class_ggplot)) <-
function(object, plot) {
object@action(plot)
}
# finds all text layers and rotates angle by 90
rotate_text_labels <- function(plot) {
layers <- plot@layers
is_text <- grepl("text", x = names(layers))
layers[is_text] <- lapply(
layers[is_text],
function(layer) {
new_layer <- ggproto(NULL, layer)
new_layer$aes_params$angle <- (new_layer$aes_params$angle %||% 0) + 90
new_layer
}
)
plot@layers <- layers
plot
}
p <- ggplot(NULL) +
geom_text(aes(x = 1, y = 1), label = "Hello World!")
mod <- mod_gg(rotate_text_labels)
p
p + mod
#> Error in `method(update_ggplot, list(mod_gg, ggplot2::ggplot))`(object = <object>, : unused argument ("mod")
update_ggplot(mod, p)
utils::sessionInfo()
#> R version 4.5.0 (2025-04-11)
#> Platform: aarch64-apple-darwin20
#> Running under: macOS Sequoia 15.6.1
#>
#> Matrix products: default
#> BLAS: /Library/Frameworks/R.framework/Versions/4.5-arm64/Resources/lib/libRblas.0.dylib
#> LAPACK: /Library/Frameworks/R.framework/Versions/4.5-arm64/Resources/lib/libRlapack.dylib; LAPACK version 3.12.1
#>
#> locale:
#> [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
#>
#> time zone: America/New_York
#> tzcode source: internal
#>
#> attached base packages:
#> [1] stats graphics grDevices utils datasets methods base
#>
#> other attached packages:
#> [1] ggplot2_4.0.0 S7_0.2.0
#>
#> loaded via a namespace (and not attached):
#> [1] vctrs_0.6.5 cli_3.6.5 knitr_1.50 rlang_1.1.6
#> [5] xfun_0.53 generics_0.1.4 labeling_0.4.3 glue_1.8.0
#> [9] htmltools_0.5.8.1 scales_1.4.0 rmarkdown_2.29 grid_4.5.0
#> [13] tibble_3.3.0 evaluate_1.0.5 fastmap_1.2.0 yaml_2.3.10
#> [17] lifecycle_1.0.4 compiler_4.5.0 dplyr_1.1.4 fs_1.6.6
#> [21] RColorBrewer_1.1-3 pkgconfig_2.0.3 farver_2.1.2 digest_0.6.37
#> [25] R6_2.6.1 tidyselect_1.2.1 reprex_2.1.1 pillar_1.11.0
#> [29] magrittr_2.0.4 tools_4.5.0 withr_3.0.2 gtable_0.3.6
Created on 2025-09-24 with reprex v2.1.1
Metadata
Metadata
Assignees
Labels
No labels