I'd like to pass a quoted string to a function that calls ggplot2.
library(magrittr); library(ggplot2)
g1 <- function( variable ) {
ggplot(mtcars, aes_string("wt", variable, size="carb")) +
geom_point()
}
g1("mpg")
This works well, but the v3.1.0 documentation advocates quasiquotation and the NSE aes()
.
All these functions are soft-deprecated. Please use tidy evaluation idioms instead (see the quasiquotation section in aes() documentation).
But the aes()
examples use NSE (ie, g1(mpg)
instead of g1("mpg")
). Likewise, these SO solutions use either NSE values or aes_()
/aes_string()
.
- Use dplyr SE with ggplot2
- How to use dplyr's enquo and quo_name in a function with tidyr and ggplot2
- Why does this aes tidyeval example from ggplot documentation throw an error?
I'd like the function to accept a SE/quoted string, to accommodate a character vector, like:
variables <- c("mpg", "cyl", "disp")
variables %>%
lapply(g1)
You can do this using the
!!
operator on the variable after call tosym
. This will unquote and evaluatevariable
in the surrounding environement.A work-around is to substitute a common name for the variable name of interest in your function: