【发布时间】:2019-03-13 00:49:29
【问题描述】:
我想将带引号的字符串传递给调用 ggplot2 的函数。
library(magrittr); library(ggplot2)
g1 <- function( variable ) {
ggplot(mtcars, aes_string("wt", variable, size="carb")) +
geom_point()
}
g1("mpg")
这很好用,但 v3.1.0 documentation 提倡准引用和 NSE aes()。
所有这些功能都已软弃用。请改用整洁的评估习语(请参阅 aes() 文档中的 quasiquotation 部分)。
但 aes() examples 使用 NSE(ie,g1(mpg) 而不是 g1("mpg"))。同样,这些 SO 解决方案使用 NSE 值或 aes_()/aes_string()。
- Use dplyr SE with ggplot2
- How to use dplyr's enquo and quo_name in a function with tidyr and ggplot2
- Why does this aes tidyeval example from ggplot documentation throw an error?
我希望该函数接受 SE/带引号的字符串,以容纳字符向量,例如:
variables <- c("mpg", "cyl", "disp")
variables %>%
lapply(g1)
【问题讨论】:
-
我了解这与@Croote 的回应之间的区别吗?这使用
ensym()而不是sym()?在稳定性方面还有其他区别或优势吗?
标签: r ggplot2 standard-evaluation