Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
395 views
in Technique[技术] by (71.8m points)

r - Programming with ggplot2 and dplyr

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 is working. Function which takes a dataframe and filters by a specified column and value.

foo <- function(df, y, t = 4){
  tmp <- df %>% 
          filter(!!enquo(y) > t)
  ggplot(tmp, aes_(substitute(y))) + 
      geom_histogram()  
}
foo(mtcars, cyl)

Now I'm trying to pipe directly to the ggplot function...gives an error

foo <- function(df, y, t=4){
  df %>% 
     filter(!!enquo(y) > t) %>% 
        ggplot(aes_(substitute(y))) + 
            geom_histogram()  
}
foo(mtcars, cyl)

Error in FUN(X[[i]], ...) : object 'cyl' not found In addition: Warning message: In FUN(X[[i]], ...) : restarting interrupted promise evaluation

and the last one. How to add a facet?

foo <- function(df, y, gr, t=4){
  df %>% 
       filter(!!enquo(y) > t) %>% 
  ggplot(aes_(substitute(y))) + 
      geom_histogram() +  
      facet_grid(~gr)
}
foo(mtcars, y= cyl, gr= vs)

Edit

The second issue can be solved using aes_q instead of aes_ & substitute. Source

foo <- function(df, y, gr, t=4){
  y <- enquo(y)
  df %>% 
    filter(!!y > t) %>% 
       ggplot(aes_q(y)) + 
           geom_histogram()
}
foo(mtcars, cyl)

Using ggplot2_2.2.1

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

ggplot2 v3.0.0 released in July 2018 supports !! (bang bang), !!!, and :=.

facet_wrap() and facet_grid() support vars() inputs. The first two arguments of facet_grid() become rows and cols. facet_grid(vars(cyl), vars(am, vs)) is equivalent to facet_grid(cyl ~ am + vs) and facet_grid(cols = vars(am, vs)) is equivalent to facet_grid(. ~ am + vs).

So your example can be modified as follow:

library(rlang)
library(tidyverse)

foo <- function(df, y, gr, t=4) {
  y <- enquo(y)
  gr <- enquo(gr)

  df %>% 
    filter(!!y > t) %>% 
    ggplot(aes(!!y)) + 
    geom_histogram() +  
    facet_grid(cols = vars(!!gr))
}

foo(mtcars, y= cyl, gr= vs)
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

Created on 2018-04-04 by the reprex package (v0.2.0).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...