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

theme(aspect.ratio=value) fails for integer values #5369

Closed
GabrielHoffman opened this issue Jul 27, 2023 · 1 comment · Fixed by #5370
Closed

theme(aspect.ratio=value) fails for integer values #5369

GabrielHoffman opened this issue Jul 27, 2023 · 1 comment · Fixed by #5370
Labels
bug an unexpected problem or unintended behavior themes 💃

Comments

@GabrielHoffman
Copy link

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
@teunbrand
Copy link
Collaborator

teunbrand commented Jul 27, 2023

Thanks for the report!
This seems to apply more broadly to all theme elements that allow numeric input, e.g.:

library(ggplot2)

ggplot(mpg, aes(displ, hwy, colour = cty)) +
  geom_point() +
  theme(legend.position = c(1L, 1L))
#> Error in `mapply()`:
#> ! The `legend.position` theme element must be a <character/numeric>
#>   object.

Created on 2023-07-27 with reprex v2.0.2

@teunbrand teunbrand added bug an unexpected problem or unintended behavior themes 💃 labels Jul 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug an unexpected problem or unintended behavior themes 💃
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants