Tidyeval splice operator !!! fails with ggplot's aes

你离开我真会死。 提交于 2020-03-03 04:38:26

问题


The article discussing tidy evaluation in ggplot2 gives the impression that aes() now supports quasiquoation. However, I'm having problems getting it to work with the unquote-splice operator !!!.

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

(Perhaps unsurprisingly) The same thing happens if the aesthetic mapping is moved to the geom:

ggplot( mtcars ) + geom_point( aes(!!v$x, !!v$y) )   # works
ggplot( mtcars ) + geom_point( aes(!!!v) )           # doesn't

Am I missing something obvious?


回答1:


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()


来源:https://stackoverflow.com/questions/55815963/tidyeval-splice-operator-fails-with-ggplots-aes

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!