问题
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