我看到ggplot v3.0.0现在支持整洁的评估。但是,这显然不允许像我使用dplyr一样将字符串对象作为变量名传递给ggplot。
y_var <- "drat"
这有效:
mtcars %>% select(!!y_var)
这不是:
ggplot(mtcars) + geom_point(aes(x = disp, y = !!y_var))
知道我在做什么错吗?
答案 0 :(得分:1)
您正在取消引用,但它仅产生一个字符向量。
这有效:
mtcars %>% select(!!y_var)
因为这样有效:
mtcars %>% select('drat')
?select
帮助实际上将其声明为例外:
# For convenience it also supports strings and character # vectors. This is unlike other verbs where strings would be # ambiguous. vars <- c(var1 = "cyl", var2 ="am") select(mtcars, !!vars) rename(mtcars, !!vars)
不能将 用作tidyverse中整洁评估的一般工作规则。
在这种情况下,aes
中的ggplot字符向量具有不同的含义,您不能仅仅给出:
ggplot(mtcars) + geom_point(aes(x = disp, y = 'drat'))
尝试例如:
ggplot(mtcars) + geom_point(aes(x = disp, y = !!as.name(y_var)))