Home > Mobile >  How to replace the deprecated ggplot2 function aes_string: accepting an arbitrary number of named st
How to replace the deprecated ggplot2 function aes_string: accepting an arbitrary number of named st

Time:11-13

aes_string had some convenient behaviours that I made use of when programming with ggplot2. But aes_string has been deprecated (noticeably since ggplot2 version 3.4.0 I believe). I am struggling with how to nicely replace it.

Specifically, I previously created functions that accepted arbitrary string arguments through the ellipsis, and passed these to aes_string via do.call, as shown in the first reprex below.

Since noticing the deprecation warning I have tried to avoid aes_string, and found myself effectively just mimicking it in a rather "hacky" looking way. Presumably, whatever flaw in aes_string led to its deprecation, would also apply to my hacky workaround. See the second reprex.

Is there a more elegant solution? I want to continue passing the variable names as strings.

Reprex of my old approach with aes_string

library(ggplot2)

plotterOld <- function(...) {
  args <- list(...)
  pointAes <- do.call(aes_string, args = args)
  ggplot(mpg, aes(displ, cty))  
    geom_point(mapping = pointAes)
}

plotterOld(colour = "cyl", size = "year")
#> Warning: `aes_string()` was deprecated in ggplot2 3.0.0.
#> ℹ Please use tidy evaluation ideoms with `aes()`

plot 1

# it can accept NULLs, and e.g. intuitively doesn't map size to anything
plotterOld(colour = "cyl", size = NULL)

plot 2

# no arguments also works fine
plotterOld()

plot 3

Created on 2022-11-11 with reprex v2.0.2


Reprex of my hacky attempt at replacing aes_string's behaviour?

library(ggplot2)

# arbitrary aesthetics passed as strings using ellipses, aes, quo and .data
myAesString <- function(...) {
  dots <- list(...)
  # early exits
  stopifnot(rlang::is_named2(dots))
  if (length(dots) == 0) {
    return(NULL)
  }
  
  # initialise empty mapping object and fill it with quosures where appropriate
  mapping <- aes()
  for (n in names(dots)) {
    v <- dots[[n]]
    if (!is.null(v)) {
      if (!rlang::is_string(v)) stop(n, " must be a string or NULL")
      mapping[[n]] <- quo(.data[[v]])
    }
  }
  return(mapping)
}

plotterNew <- function(...) {
  pointAes <- myAesString(...)
  ggplot(mpg, aes(displ, cty))  
    geom_point(mapping = pointAes)
}

plotterNew(colour = "cyl", size = "year")

plot 4

plotterNew(colour = "cyl", size = NULL, shape = "drv")

plot 5

plotterNew()

plot 6


# seems to work fine
p <- plotterNew(colour = "cyl", size = "year")
p$layers[[1]]$mapping
#> Aesthetic mapping: 
#> * `colour` -> `.data[["cyl"]]`
#> * `size`   -> `.data[["year"]]`

Created on 2022-11-11 with reprex v2.0.2

Session info
sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value
#>  version  R version 4.2.1 (2022-06-23)
#>  os       macOS Big Sur ... 10.16
#>  system   x86_64, darwin17.0
#>  ui       X11
#>  language (EN)
#>  collate  en_GB.UTF-8
#>  ctype    en_GB.UTF-8
#>  tz       Europe/Amsterdam
#>  date     2022-11-11
#>  pandoc   2.18 @ /Applications/RStudio.app/Contents/MacOS/quarto/bin/tools/ (via rmarkdown)
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package     * version date (UTC) lib source
#>  assertthat    0.2.1   2019-03-21 [1] CRAN (R 4.2.0)
#>  cli           3.4.1   2022-09-23 [1] CRAN (R 4.2.0)
#>  colorspace    2.0-3   2022-02-21 [1] CRAN (R 4.2.0)
#>  curl          4.3.3   2022-10-06 [1] CRAN (R 4.2.0)
#>  DBI           1.1.3   2022-06-18 [1] CRAN (R 4.2.0)
#>  digest        0.6.30  2022-10-18 [1] CRAN (R 4.2.1)
#>  dplyr         1.0.10  2022-09-01 [1] CRAN (R 4.2.0)
#>  evaluate      0.18    2022-11-07 [1] CRAN (R 4.2.0)
#>  fansi         1.0.3   2022-03-24 [1] CRAN (R 4.2.0)
#>  farver        2.1.1   2022-07-06 [1] CRAN (R 4.2.0)
#>  fastmap       1.1.0   2021-01-25 [1] RSPM (R 4.2.0)
#>  fs            1.5.2   2021-12-08 [1] RSPM (R 4.2.0)
#>  generics      0.1.3   2022-07-05 [1] CRAN (R 4.2.0)
#>  ggplot2     * 3.4.0   2022-11-04 [1] CRAN (R 4.2.1)
#>  glue          1.6.2   2022-02-24 [1] CRAN (R 4.2.0)
#>  gtable        0.3.1   2022-09-01 [1] CRAN (R 4.2.0)
#>  highr         0.9     2021-04-16 [1] RSPM (R 4.2.0)
#>  htmltools     0.5.3   2022-07-18 [1] CRAN (R 4.2.0)
#>  httr          1.4.4   2022-08-17 [1] CRAN (R 4.2.0)
#>  knitr         1.40    2022-08-24 [1] CRAN (R 4.2.0)
#>  labeling      0.4.2   2020-10-20 [1] CRAN (R 4.2.0)
#>  lifecycle     1.0.3   2022-10-07 [1] CRAN (R 4.2.0)
#>  magrittr      2.0.3   2022-03-30 [1] CRAN (R 4.2.0)
#>  mime          0.12    2021-09-28 [1] RSPM (R 4.2.0)
#>  munsell       0.5.0   2018-06-12 [1] CRAN (R 4.2.0)
#>  pillar        1.8.1   2022-08-19 [1] CRAN (R 4.2.0)
#>  pkgconfig     2.0.3   2019-09-22 [1] CRAN (R 4.2.0)
#>  purrr         0.3.5   2022-10-06 [1] CRAN (R 4.2.0)
#>  R.cache       0.16.0  2022-07-21 [1] CRAN (R 4.2.0)
#>  R.methodsS3   1.8.2   2022-06-13 [1] CRAN (R 4.2.0)
#>  R.oo          1.25.0  2022-06-12 [1] CRAN (R 4.2.0)
#>  R.utils       2.12.1  2022-10-30 [1] CRAN (R 4.2.0)
#>  R6            2.5.1   2021-08-19 [1] CRAN (R 4.2.0)
#>  reprex        2.0.2   2022-08-17 [1] CRAN (R 4.2.0)
#>  rlang         1.0.6   2022-09-24 [1] CRAN (R 4.2.0)
#>  rmarkdown     2.18    2022-11-09 [1] CRAN (R 4.2.1)
#>  rstudioapi    0.14    2022-08-22 [1] CRAN (R 4.2.0)
#>  scales        1.2.1   2022-08-20 [1] CRAN (R 4.2.0)
#>  sessioninfo   1.2.2   2021-12-06 [1] RSPM (R 4.2.0)
#>  stringi       1.7.8   2022-07-11 [1] CRAN (R 4.2.0)
#>  stringr       1.4.1   2022-08-20 [1] CRAN (R 4.2.0)
#>  styler        1.8.1   2022-11-07 [1] CRAN (R 4.2.0)
#>  tibble        3.1.8   2022-07-22 [1] CRAN (R 4.2.0)
#>  tidyselect    1.2.0   2022-10-10 [1] CRAN (R 4.2.0)
#>  utf8          1.2.2   2021-07-24 [1] CRAN (R 4.2.0)
#>  vctrs         0.5.0   2022-10-22 [1] CRAN (R 4.2.0)
#>  withr         2.5.0   2022-03-03 [1] CRAN (R 4.2.0)
#>  xfun          0.34    2022-10-18 [1] CRAN (R 4.2.0)
#>  xml2          1.3.3   2021-11-30 [1] RSPM (R 4.2.0)
#>  yaml          2.3.6   2022-10-18 [1] CRAN (R 4.2.1)
#> 
#>  [1] /Library/Frameworks/R.framework/Versions/4.2/Resources/library
#> 
#> ──────────────────────────────────────────────────────────────────────────────
```

CodePudding user response:

One option would be to convert your list of quoted strings to symbols using sym:

library(ggplot2)

plotterOld <- function(...) {
  args <- lapply(list(...), function(x) if (!is.null(x)) sym(x))
  
  pointAes <- do.call(aes, args = args)
  ggplot(mpg, aes(displ, cty))  
    geom_point(mapping = pointAes)
}

UPDATE And we could simplify even further by using !!! to get rid of do.call:

plotterOld <- function(...) {
  args <- lapply(list(...), function(x) if (!is.null(x)) sym(x))
  
  ggplot(mpg, aes(displ, cty))  
    geom_point(mapping = aes(!!!args))
}
plotterOld(colour = "cyl", size = "year")


plotterOld(colour = "cyl", size = NULL)


plotterOld()

CodePudding user response:

You can use ensyms to convert named string arguments to named symbol arguments, so the equivalent to your old plotting function could be

library(ggplot2)

plotterNew <- function(...) {
  ggplot(mpg, aes(displ, cty))  
    geom_point(mapping = aes(!!!ensyms(...)))
}

plotterNew(colour = "cyl", size = "year")

Created on 2022-11-12 with reprex v2.0.2

  • Related