Give variables to ggplot2 programmatically (works for v 3.0.0 on)
# for a single column
require(tidyverse)
plot_iris <- function(my_y_col) {
my_y_col <- enquo(my_y_col) # intercept and pass on the expression rather than evaluating
ggplot(iris, aes(x = Species, y = !!my_y_col)) + geom_boxplot() # !! unpackage what was enquo'd
}
plot_iris(Petal.Length)
# for multiple columns:
my_function <- function(filter_col, filter_val) {
df <- diamonds
for (n in 1:length(filter_col)) {
df <- df %>%
filter(!!filter_col[[n]] == filter_val[[n]])
}
return(df)
}
my_function(c(quo(cut), quo(color)), c("Good", "E"))