你可以使用
tidyeval
引入的方法
ggplot2 v3.0.0
# install.packages("ggplot2", dependencies = TRUE)
library(ggplot2)
var1 <- "wt"
var2 <- "mpg"
multiplier <- 10
ggplot(data = subset(mtcars, cyl == 4),
aes(x = !! rlang::sym(var1), y = !! rlang::sym(var2))) +
geom_line(size = 1.5, color = "#00868B") +
geom_line(data = subset(mtcars, cyl == 8),
aes(x = !! rlang::sym(var1), y = !! rlang::sym(var2) * multiplier))
或者把一切都放在一个函数中
plot_select_vars <- function(var1, var2, multiplier) {
var1 <- rlang::sym(var1)
var2 <- rlang::sym(var2)
ggplot(data = subset(mtcars, cyl == 4),
aes(x = !! var1, y = !! var2)) +
geom_line(size = 1.5, color = "#00868B") +
geom_line(data = subset(mtcars, cyl == 8),
aes(x = !! var1, y = !! var2 * multiplier))
}
plot_select_vars(var1, var2, multiplier)
于2018-06-06由
reprex package
(b)。