我怎样才能制作一个函数,它需要一个列并在 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()
}