【问题标题】:Curly Curly on ggplot labelggplot标签上的卷曲卷曲
【发布时间】:2021-08-20 13:11:59
【问题描述】:

我在 ggplot 的实验室中使用 curly curly,它似乎不起作用,如何在尝试 #2 中的 ggplot 实验室中使用标签。对于 Try#3,我想通过函数中的 purr 传递所有数值变量。

尝试 #1 - 这行得通

library(tidyverse)

myplot <- function(df,col) {
  df %>% 
  ggplot(aes(x={{col}}))+
  geom_histogram()+
   # labs(title=paste0("Histogram for ",{{col}}))+
    theme_classic()
}

myplot(iris,Sepal.Length)

尝试 #2 - 这不起作用

myplot <- function(df,col) {
  df %>% 
  ggplot(aes(x={{col}}))+
  geom_histogram()+
   labs(title=paste0("Histogram for ",{{col}}))+
    theme_classic()
}

myplot(iris,Sepal.Length)

尝试 #3 - 这也不起作用

myplot <- function(df,col) {
  df %>% 
  ggplot(aes(x={{col}}))+
  geom_histogram()+
  theme_classic()
}

iris[,-5] %>% 
  map(myplot)

【问题讨论】:

    标签: r ggplot2 dplyr tidyverse


    【解决方案1】:

    如果您可以将列名作为字符串传递。

    library(ggplot2)
    library(purrr)
    
    myplot <- function(df,col) {
      df %>% 
        ggplot(aes(x=.data[[col]]))+
        geom_histogram()+
        labs(title=paste0("Histogram for ",col))+
        theme_classic()
    }
    
    list_plot <- map(names(iris)[-5], ~myplot(iris, .))
    

    【讨论】:

    • 谢谢,完美,如果不是作为字符串传递并通过 {{ in ggplot aesthethics 进行捕获,则可以捕获任何解决方案
    • 您可以将变量作为符号传递给同一个函数。 list_plot1 &lt;- map(syms(names(iris)[-5]), ~myplot(iris, .))。我不确定这是否有帮助。
    【解决方案2】:

    您可以使用老式的deparse(substitute(...)) 将符号捕获为文本。

    library(tidyverse)
    
    myplot <- function(df,col) {
      df %>% 
        ggplot(aes(x={{col}}))+
        geom_histogram()+
        labs(title=paste0("Histogram for ", deparse(substitute(col))))+
        theme_classic()
    }
    
    myplot(iris,Sepal.Length)
    #> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
    

    由reprex package (v1.0.0) 于 2021-08-20 创建

    【讨论】:

    • 谢谢,关于如何通过 map 函数传递所有数字列而不是为所有数字列单独调用此函数
    • 不,我的咕噜声不流利
    【解决方案3】:

    另一种选择是使用rlang::as_label(rlang::enquo(col)):

    library(ggplot2)
    
    myplot <- function(df, col) {
      ggplot(df, aes(x={{col}}))+
        geom_histogram()+
        labs(title=paste0("Histogram for ", rlang::as_label(rlang::enquo(col)))) +
        theme_classic()
    }
    
    myplot(iris,Sepal.Length)
    #> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
    

    如果您想遍历多个列,一种选择是稍微重写您的函数以利用来自rlang 的.data 代词并遍历列名:

    myplot2 <- function(df, col) {
      ggplot(df, aes(x = .data[[col]]))+
        geom_histogram()+
        labs(title=paste0("Histogram for ", col)) +
        theme_classic()
    }
    
    p <- purrr::map(names(iris[,-5]), myplot2, df = iris)
    
    p[[2]]
    #> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
    

    【讨论】:

    • 好奇 col 在实验室中是如何工作的,但不在 myplot2 以下
    • 另外,我如何解释通过映射 df 传递给函数的参数的顺序是 iris,names(iris[-5]) 是否作为 col 传递给 myplot?
    • 嗯。对于你的第一个问题。 col 在您的环境中既不是角色也不是对象。这就是为什么您的代码不起作用的原因。它只存在于数据框中。这就是{{ 正在做的事情。它告诉 R 在你的 df 中寻找 col。然而,这并不适用于所有地方。有关这方面的更多信息,我建议您查看例如tidyeval.tidyverse.org/multiple.html。对于你的第二个问题。地图(名称(虹膜[-5]),...)。在某种意义上是在做for (col in names(iris[-5])),即循环遍历列名并将它们一对一传递给您的函数。
    猜你喜欢
    • 2012-12-15
    • 2015-02-01
    • 1970-01-01
    • 2010-12-10
    • 2017-02-05
    • 2022-08-13
    • 2012-12-15
    • 2017-03-27
    • 1970-01-01
    相关资源
    最近更新 更多