【发布时间】:2017-12-29 23:06:12
【问题描述】:
我经常在包装函数中将dplyr 与ggplot2 结合起来进行分析。当我使用tidyeval 迁移到 v.0.7.1 的新 NSE / SE 范例时,我正在努力让这种组合发挥作用。我发现ggplot 不理解未引用的quosers(还)。以下方法不起作用:
example_func <- function(col) {
col <- enquo(col)
mtcars %>% count(!!col) %>%
ggplot(aes((!!col), n)) +
geom_bar(stat = "identity")
}
example_func(cyl)
# Error in !col : invalid argument type
我目前使用以下解决方法。但我认为一定有更好的方法。
example_func2 <- function(col) {
col <- enquo(col)
mtcars %>% count(!!col) %>%
ggplot(aes_string(rlang::quo_text(col), "n")) +
geom_bar(stat = "identity")
}
请告诉我将这两者结合起来的最佳方式。谢谢!
【问题讨论】:
-
如果是字符串,需要
aes_string,那是什么问题? -
感觉绕了一大圈,先引用,然后用quo_text,再用aes_string。希望有更直接的解决方案。
-
然而,这个 seems to be the way to go 在使用 tidyeval 时。