【问题标题】:passing unquoted variables to curly curly {{}} ggplot function将不带引号的变量传递给 curly curly {{}} ggplot 函数
【发布时间】:2019-12-02 15:36:17
【问题描述】:

这来自我问过recently 的另一个问题。据我了解,来自 rlang 的 curly curly {{}} 在函数内使用并与 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)

(输出应该是三个不同的图,第一个看起来像第一个示例中的图。)

【问题讨论】:

  • thisthis
  • @IceCreamToucan 谢谢,但这些示例试图传递带引号的变量,我正在尝试使用 curly curly 做相反的事情,即传递未带引号的变量,我认为最新版本的 ggplot 可以做到这一点正如here 解释的那样,“如果列名或表达式由用户提供,您也可以使用 {{ col }} 将其传递给 aes() 或 vars()”
  • 也许你可以举一个你实际输入的例子?如果您确实有 noquote 类的输入(如 vars 对象),您可以使用 as.character 转换为字符,以便链接的解决方案可以工作/应用。不过老实说,我认为您的输入实际上不太可能是 noquote 类对象。看来您只是误解了“未引用”在这种情况下的含义。 noquote 所做的只是改变字符对象在打印时的外观,以便元素周围没有引号。这种“引用”与tidyeval上下文中的“引用”是不一样的。
  • 啊,我明白了,我只是在想有没有办法在函数中使用 curly curly(而不是 .data[[col]])并将它的向量/列表传递给 map/lapply(所以真的把首先将向量导入其他内容 - 正如您所建议的那样使用 as.character 可能是我所追求的),而不是像我在工作示例中所做的那样只传递一个变量。

标签: r ggplot2 rlang


【解决方案1】:

非标准评估很棘手。这里的关键是您需要一个符号列表,而不是字符向量,然后您还需要在 map() 调用中使用 bang-bang 运算符 (!!),因为您要捕获 @987654324 的值@,而不是 .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() 输出一个字符串(仅属于 '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

【讨论】:

    猜你喜欢
    • 2020-12-06
    • 1970-01-01
    • 1970-01-01
    • 2021-02-11
    • 2021-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-25
    相关资源
    最近更新 更多