Tidyeval接线员!ggplot的AES失败

Art*_*lov 2 r ggplot2 tidyeval

讨论ggplot2中的整洁评估的文章给人的印象是aes()现在支持准化。但是,我无法使其与unquote-splice运算符一起使用!!!

library( ggplot2 )

## Predefine the mapping of symbols to aesthetics
v <- rlang::exprs( x=wt, y=mpg )

## Symbol-by-symbol unquoting works without problems
ggplot( mtcars, aes(!!v$x, !!v$y) ) + geom_point()

## But unquote splicing doesn't...
ggplot( mtcars, aes(!!!v) ) + geom_point()
# Error: Can't use `!!!` at top level
# Call `rlang::last_error()` to see a backtrace
Run Code Online (Sandbox Code Playgroud)

(也许不足为奇)如果将美学贴图移动到几何体,则会发生相同的事情:

ggplot( mtcars ) + geom_point( aes(!!v$x, !!v$y) )   # works
ggplot( mtcars ) + geom_point( aes(!!!v) )           # doesn't
Run Code Online (Sandbox Code Playgroud)

我是否缺少明显的东西?

Lio*_*nry 5

That's because aes() takes x and y arguments and !!! only works within dots. We'll try to solve this particular problem in the future. In the interim you'll need to unquote x and y individually, or use the following workaround:

aes2 <- function(...) {
  eval(expr(aes(!!!enquos(...))))
}

ggplot(mtcars, aes2(!!!v)) + geom_point()
Run Code Online (Sandbox Code Playgroud)