-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Closed
Labels
Description
I found a problem with ggplot2()
using theme(aspect.ratio=value)
. Plotting fails if value
is an integer
Here is the code to reproduce the bug:
library(ggplot2)
# set the aspect.ratio
ratio = nrow(sleep)
# the ratio is an "integer"
is(ratio)
#> [1] "integer" "double" "numeric"
#> [4] "vector" "data.frameRowLabels"
# Fails
ggplot(sleep, aes(group, ID, fill=extra)) +
geom_tile() +
theme(aspect.ratio=ratio)
#> Error in `mapply()`:
#> ! The `aspect.ratio` theme element must be a <numeric> object.
#> Backtrace:
#> ▆
#> 1. ├─base::tryCatch(...)
#> 2. │ └─base (local) tryCatchList(expr, classes, parentenv, handlers)
#> 3. │ ├─base (local) tryCatchOne(...)
#> 4. │ │ └─base (local) doTryCatch(return(expr), name, parentenv, handler)
#> 5. │ └─base (local) tryCatchList(expr, names[-nh], parentenv, handlers[-nh])
#> 6. │ └─base (local) tryCatchOne(expr, names, parentenv, handlers[[1L]])
#> 7. │ └─base (local) doTryCatch(return(expr), name, parentenv, handler)
#> 8. ├─base::withCallingHandlers(...)
#> 9. ├─base::saveRDS(...)
#> 10. ├─base::do.call(...)
#> 11. ├─base (local) `<fn>`(...)
#> 12. └─global `<fn>`(input = base::quote("milky-cub_reprex.R"))
#> 13. └─rmarkdown::render(input, quiet = TRUE, envir = globalenv(), encoding = "UTF-8")
#> 14. └─knitr::knit(knit_input, knit_output, envir = envir, quiet = quiet)
#> 15. └─knitr:::process_file(text, output)
#> 16. ├─base::withCallingHandlers(...)
#> 17. ├─base::withCallingHandlers(...)
#> 18. ├─knitr:::process_group(group)
#> 19. └─knitr:::process_group.block(group)
#> 20. └─knitr:::call_block(x)
#> 21. └─knitr:::block_exec(params)
#> 22. └─knitr:::eng_r(options)
#> 23. ├─knitr:::in_input_dir(...)
#> 24. │ └─knitr:::in_dir(input_dir(), expr)
#> 25. └─knitr (local) evaluate(...)
#> 26. └─evaluate::evaluate(...)
#> 27. └─evaluate:::evaluate_call(...)
#> 28. ├─evaluate (local) handle(...)
#> 29. │ └─base::try(f, silent = TRUE)
#> 30. │ └─base::tryCatch(...)
#> 31. │ └─base (local) tryCatchList(expr, classes, parentenv, handlers)
#> 32. │ └─base (local) tryCatchOne(expr, names, parentenv, handlers[[1L]])
#> 33. │ └─base (local) doTryCatch(return(expr), name, parentenv, handler)
#> 34. ├─base::withCallingHandlers(...)
#> 35. ├─base::withVisible(value_fun(ev$value, ev$visible))
#> 36. └─knitr (local) value_fun(ev$value, ev$visible)
#> 37. └─knitr (local) fun(x, options = options)
#> 38. ├─base::withVisible(knit_print(x, ...))
#> 39. ├─knitr::knit_print(x, ...)
#> 40. └─knitr:::knit_print.default(x, ...)
#> 41. └─evaluate (local) normal_print(x)
#> 42. ├─base::print(x)
#> 43. └─ggplot2:::print.ggplot(x)
#> 44. ├─ggplot2::ggplot_gtable(data)
#> 45. └─ggplot2:::ggplot_gtable.ggplot_built(data)
#> 46. └─ggplot2:::plot_theme(plot)
#> 47. └─base::mapply(validate_element, theme, names(theme), MoreArgs = list(element_tree = get_element_tree()))
#> 48. └─ggplot2 (local) `<fn>`(dots[[1L]][[5L]], dots[[2L]][[5L]], element_tree = `<named list>`)
#> 49. └─cli::cli_abort(...)
#> 50. └─rlang::abort(...)
# Success after casting ratio to be "numeric"
ggplot(sleep, aes(group, ID, fill=extra)) +
geom_tile() +
theme(aspect.ratio=as.numeric(ratio))
sessionInfo()
#> R version 4.3.0 (2023-04-21)
#> Platform: x86_64-apple-darwin22.4.0 (64-bit)
#> Running under: macOS Ventura 13.4
#>
#> Matrix products: default
#> BLAS: /Users/gabrielhoffman/prog/R-4.3.0/lib/libRblas.dylib
#> LAPACK: /usr/local/Cellar/r/4.3.0_1/lib/R/lib/libRlapack.dylib; LAPACK version 3.11.0
#>
#> 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_3.4.2
#>
#> loaded via a namespace (and not attached):
#> [1] vctrs_0.6.3 cli_3.6.1 knitr_1.43 rlang_1.1.1
#> [5] xfun_0.39 generics_0.1.3 labeling_0.4.2 glue_1.6.2
#> [9] colorspace_2.1-0 htmltools_0.5.5 scales_1.2.1 fansi_1.0.4
#> [13] rmarkdown_2.22 grid_4.3.0 evaluate_0.21 munsell_0.5.0
#> [17] tibble_3.2.1 fastmap_1.1.1 yaml_2.3.7 lifecycle_1.0.3
#> [21] compiler_4.3.0 dplyr_1.1.2 fs_1.6.2 pkgconfig_2.0.3
#> [25] farver_2.1.1 digest_0.6.31 R6_2.5.1 tidyselect_1.2.0
#> [29] reprex_2.0.2 utf8_1.2.3 pillar_1.9.0 magrittr_2.0.3
#> [33] tools_4.3.0 withr_2.5.0 gtable_0.3.3