Tidyeval 拼接运算符 !!! ggplot 的 aes 失败
Tidyeval splice operator !!! fails with ggplot's aes
The article discussing tidy evaluation in 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
(也许不足为奇)如果将美学映射移动到 geom,也会发生同样的事情:
ggplot( mtcars ) + geom_point( aes(!!v$x, !!v$y) ) # works
ggplot( mtcars ) + geom_point( aes(!!!v) ) # doesn't
我是不是遗漏了什么明显的东西?
那是因为 aes()
采用 x
和 y
参数,而 !!!
仅在点内有效。我们将在未来尝试解决这个特殊问题。在此期间,您需要单独取消引用 x
和 y
,或使用以下解决方法:
aes2 <- function(...) {
eval(expr(aes(!!!enquos(...))))
}
ggplot(mtcars, aes2(!!!v)) + geom_point()
随着新的 rlang 更新,我认为您可以使用 {{}} 在 ggplot 中工作
polo<- function(x,y, data, by) {
ggplot({{data}}, aes(x = {{x}}, y = {{y}})) +
geom_point() +
facet_grid(rows = vars({{by}}))
}
polo(data = mtcars, x = cyl, y = mpg, by = vs )
我面临的唯一问题是使用 facet_wrap。我试过 quo_name 但这似乎不适用于 facet_wrap(~ quo_name(by))。否则一切正常。虽然这作为解决方法也有帮助,但我更喜欢坚持使用 facet_grid 和 varsworkaround.
作为解决方法 (first described by Hadley),以下代码使用空参数来跳过 aes
中的命名参数。这样,!!!
在 aes
:
中工作
ggplot(mtcars) + geom_point(aes(, , !!! v))
The article discussing tidy evaluation in 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
(也许不足为奇)如果将美学映射移动到 geom,也会发生同样的事情:
ggplot( mtcars ) + geom_point( aes(!!v$x, !!v$y) ) # works
ggplot( mtcars ) + geom_point( aes(!!!v) ) # doesn't
我是不是遗漏了什么明显的东西?
那是因为 aes()
采用 x
和 y
参数,而 !!!
仅在点内有效。我们将在未来尝试解决这个特殊问题。在此期间,您需要单独取消引用 x
和 y
,或使用以下解决方法:
aes2 <- function(...) {
eval(expr(aes(!!!enquos(...))))
}
ggplot(mtcars, aes2(!!!v)) + geom_point()
随着新的 rlang 更新,我认为您可以使用 {{}} 在 ggplot 中工作
polo<- function(x,y, data, by) {
ggplot({{data}}, aes(x = {{x}}, y = {{y}})) +
geom_point() +
facet_grid(rows = vars({{by}}))
}
polo(data = mtcars, x = cyl, y = mpg, by = vs )
我面临的唯一问题是使用 facet_wrap。我试过 quo_name 但这似乎不适用于 facet_wrap(~ quo_name(by))。否则一切正常。虽然这作为解决方法也有帮助,但我更喜欢坚持使用 facet_grid 和 varsworkaround.
作为解决方法 (first described by Hadley),以下代码使用空参数来跳过 aes
中的命名参数。这样,!!!
在 aes
:
ggplot(mtcars) + geom_point(aes(, , !!! v))