将不带引号的变量传递给卷曲的{{}} ggplot函数 [英] passing unquoted variables to curly curly {{}} ggplot function

查看:38
本文介绍了将不带引号的变量传递给卷曲的{{}} ggplot函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这来自于我问过的另一个问题

我的问题是,当我取消引用要循环遍历的变量时,为什么以下内容不起作用?

  #variables循环浏览vars1<-c("cut","color","clarity")#未加引号的变量vars<-noquote(vars1)瓦斯#[1]切割颜色清晰度plot_list<-vars%>%map(.,〜plot_f(钻石,.x,深度))#plots,但填充不正确grid.arrange(grobs = plot_list,ncol = 1) 

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

解决方案

非标准评估很棘手.这里的关键是您需要一个符号列表,而不是一个字符向量,然后还需要在 map()中使用bang-bang运算符( !! )调用,因为您要捕获 .x 的值,而不是 .x 本身的值.

 库(tidyverse)图书馆(cowplot)图书馆(gridExtra)库(rlang)#plot函数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()}#要循环遍历的变量vars1<-c("cut","color","clarity")#转换为符号vars<-syms(vars1)#注意!在.x前面plot_list<-vars%>%map(.,〜plot_f(钻石,!!.x,深度))grid.arrange(grobs = plot_list,ncol = 1) 

请注意, noquote()输出一个字符串(仅属于'noquote'类),但是您需要一个符号列表.

  vars<-noquote(vars1)str(变量)#>'noquote'chr [1:3]剪切"颜色"清晰度"vars<-syms(vars1)str(变量)#>清单3#>$:符号切割#>$:符号颜色#>$:符号清晰度 

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.)

解决方案

Nonstandard evaluation is tricky. The key here is that you need a list of symbols, not a character vector, and then you also need to use the bang-bang operator (!!) in your map() call because you want to capture the value of .x, not .x itself.

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)

Note that noquote() outputs a character string (just of class 'noquote'), but you need a list of symbols.

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

这篇关于将不带引号的变量传递给卷曲的{{}} ggplot函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆