1

我怎样才能制作一个函数,它需要一个列并在 dplyr、tidyr 和 ggplot 中使用它?

df <- data.frame(date_col = c(1,1,2,2,3,4,4,5,5), 
                 col_a = c('a','b','a','b','a','a','b','a','b'),
                 val_col = runif(9))

如何编写函数接受参数param_col而不是硬编码的 col_a

df %>% 
  group_by(date_col, col_a) %>% 
  summarise(val_col = sum(val_col)) %>% 
  complete(col_a, date_col) %>% 
  ggplot(aes(date_col, val_col, color = col_a)) + 
  geom_line() 

dplyr 和 ggplot 调用在下面概述的代码中工作。但是complete调用应该怎么写呢?还是应该complete_使用?

有没有更规范的方式来做到这一点?

plot_nice_chart <- function(df, param_col) {

  enq_param_col <- enquo(param_col)
  str_param_col <- deparse(substitute(param_col))


  # aggregate data based on group_by_col, 
  # explicitly fill in NA's for missing to avoid interpolation
  df %>% 
     group_by(!!enq_param_col, date_col) %>%
     summarise(val_col = sum(val_col)) %>%
     complete(<what-should-be-here?>, date_col) %>%
     ggplot(aes_string("date_col", "val_col", color = str_param_col)) +
        geom_line()
}
4

1 回答 1

1

tidyr的开发版本tidyr_0.6.3.9000现在tidyeval使用!!.group_by

plot_nice_chart <- function(df, param_col) {

     enq_param_col <- enquo(param_col)
     str_param_col <- deparse(substitute(param_col))
     str_param_col
     df %>%
          group_by(!!enq_param_col, date_col) %>%
          summarise(val_col = sum(val_col)) %>%
          ungroup() %>%
          complete(!!enq_param_col, date_col) %>%
          ggplot(aes_string("date_col", "val_col", color = str_param_col)) +
          geom_line()
}

使用当前版本,您可以将complete_变量用作字符串。

plot_nice_chart <- function(df, param_col) {

     enq_param_col <- enquo(param_col)
     str_param_col <- deparse(substitute(param_col))

     df %>%
          group_by(!!enq_param_col, date_col) %>%
          summarise(val_col = sum(val_col)) %>%
          ungroup() %>%
          complete_( c(str_param_col, "date_col") ) %>%
          ggplot(aes_string("date_col", "val_col", color = str_param_col)) +
          geom_line()
}
于 2017-07-05T17:04:24.653 回答