将不带引号的变量传递给 curly curly {{}} ggplot 函数
passing unquoted variables to curly curly {{}} ggplot function
这来自我问 . As I understand curly curly {{}}
from rlang
is used within functions and with unquoted 变量的另一个问题,因此以下函数和调用有效:
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
我的问题是,当我取消引用要循环的变量时,为什么以下内容不起作用?
#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)
(输出应该是三个不同的图,第一个看起来像第一个示例中的图。)
非标准评估很棘手。这里的关键是您需要一个符号列表,而不是字符向量,然后您还需要在 map()
调用中使用 bang-bang 运算符 (!!
),因为您想要捕获.x
的值,而不是 .x
本身。
library(tidyverse)
library(cowplot)
library(gridExtra)
library(rlang)
#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()
}
#variables to cycle through
vars1 <- c("cut", "color", "clarity")
# convert to symbols
vars <- syms(vars1)
# note the !! in front of .x
plot_list <- vars %>%
map(., ~plot_f(diamonds, !!.x, depth))
grid.arrange(grobs = plot_list, ncol = 1)
请注意,noquote()
输出一个字符串(只是 class 'noquote'
),但您需要一个符号列表。
vars <- noquote(vars1)
str(vars)
#> 'noquote' chr [1:3] "cut" "color" "clarity"
vars <- syms(vars1)
str(vars)
#> List of 3
#> $ : symbol cut
#> $ : symbol color
#> $ : symbol clarity
这来自我问 {{}}
from rlang
is used within functions and with unquoted 变量的另一个问题,因此以下函数和调用有效:
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
我的问题是,当我取消引用要循环的变量时,为什么以下内容不起作用?
#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)
(输出应该是三个不同的图,第一个看起来像第一个示例中的图。)
非标准评估很棘手。这里的关键是您需要一个符号列表,而不是字符向量,然后您还需要在 map()
调用中使用 bang-bang 运算符 (!!
),因为您想要捕获.x
的值,而不是 .x
本身。
library(tidyverse)
library(cowplot)
library(gridExtra)
library(rlang)
#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()
}
#variables to cycle through
vars1 <- c("cut", "color", "clarity")
# convert to symbols
vars <- syms(vars1)
# note the !! in front of .x
plot_list <- vars %>%
map(., ~plot_f(diamonds, !!.x, depth))
grid.arrange(grobs = plot_list, ncol = 1)
请注意,noquote()
输出一个字符串(只是 class 'noquote'
),但您需要一个符号列表。
vars <- noquote(vars1)
str(vars)
#> 'noquote' chr [1:3] "cut" "color" "clarity"
vars <- syms(vars1)
str(vars)
#> List of 3
#> $ : symbol cut
#> $ : symbol color
#> $ : symbol clarity