Programming with ggplot2 and dplyr

后端 未结 2 1137
-上瘾入骨i
-上瘾入骨i 2021-01-01 06:32

I want to combine dplyr and ggplot within one function using piping and struggling with some issues now.

Here is the first easy one which

2条回答
  •  一个人的身影
    2021-01-01 07:25

    @Tung's answer can be simplified using {{ syntax. https://rlang.r-lib.org/reference/quasiquotation.html

    library(rlang)
    library(dplyr)
    library(ggplot2)
    
    foo <- function(df, y, gr, t = 4) {
      df %>% 
        filter({{ y }}> t) %>% 
        ggplot(aes({{ y }})) + 
        geom_histogram() +  
        facet_grid(cols = vars({{ gr }}))
    }
    
    foo(mtcars, y = cyl, gr = vs)
    

提交回复
热议问题