This comes from a different question I asked recently. As I understand curly curly {{}}
from rlang
is used within functions and with unquoted variables so the following function and call works:
library(ggplot2)
library(tidyverse)
library(cowplot)
library(gridExtra)
#plot function
plot_f <- function(df, x_var, y_var) {
ggplot(df, aes(x = {{x_var}}, y = {{y_var}})) + geom_boxplot(aes(fill = {{x_var}})) +
theme_cowplot()
}
#pass unquoted variable
plot_f(diamonds, cut, depth) #plots fine
My question is why does the following not work when I unquote the variables I want to cycle through?
#variables to cycle through
vars1 <- c("cut", "color", "clarity")
#unquoted variables
vars <- noquote(vars1)
vars
#[1] cut color clarity
plot_list <- vars %>%
map(., ~plot_f(diamonds, .x, depth))
#plots but fill isn't correct
grid.arrange(grobs = plot_list, ncol = 1)
(The output should be three different plots, with the first one looking like the plot from the first example.)
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…