dplyr函数,仅当提供参数时才具有可选分组

时间:2018-10-16 12:34:49

标签: r dplyr metaprogramming tidyverse quasiquotes

我需要编写一个dplyr函数来创建自定义面积图。所以这是我的尝试。

area_plot <- function(data, what, by){
  by <- ensym(by)
  what <- ensym(what)

  data %>% 
    filter(!is.na(!!by)) %>% 
    group_by(date, !!by) %>% 
    summarise(!!what := sum(!!what, na.rm = TRUE)) %>% 
    complete(date, !!by, fill = rlang::list2(!!what := 0)) %>% 
    ggplot(aes(date, !!what, fill = !!by)) +
    geom_area(position = 'stack') +
    scale_x_date(breaks = '1 month', date_labels = '%Y-%m', expand = c(.01, .01)) +
    theme_minimal() +
    theme(axis.text.x = element_text(angle = 90, vjust = .4)) +
    labs(fill = '')
}

但是我一直想知道by参数是否有任何默认值,该默认值将为所有组一起输出geom_area图。我知道我可以先使用if来定义ggplot2中使用的数据,然后在函数中执行以下操作:

if (by != 'default') {
    data <- data %>% 
    filter(!is.na(!!by)) %>% 
    group_by(date, !!by) %>% 
    summarise(!!what := sum(!!what, na.rm = TRUE)) %>% 
    complete(date, !!by, fill = rlang::list2(!!what := 0))}

ggplot(data, aes(date, !!what, fill = !!by)) +
geom_area(position = 'stack') +
scale_x_date(breaks = '1 month', date_labels = '%Y-%m', expand = c(.01, .01)) +
theme_minimal() +
theme(axis.text.x = element_text(angle = 90, vjust = .4)) +
labs(fill = '')

但是,我想知道是否有个巧妙的窍门可以为group_by提供一些值(例如常量),这将使summarise保持原始结构(因此,基本上什么也不做),尽管被调用了。类似于在ggplot2中为某种美学提供常量时的行为。

请参阅所附数据的样本。 group是可选的分组变量。

structure(list(date = structure(c(17052, 17654, 17111, 17402, 
17090, 17765, 17181, 17301, 17496, 17051, 16980, 17155, 17599, 
16986, 17607, 17620, 17328, 17085, 17666, 17759, 17238, 16975, 
17242, 17322, 17625, 17598, 17124, 17648, 17675, 17613, 17044, 
16984, 16968, 17421, 17152, 17148, 17418, 17017, 17655, 17148, 
16981, 17644, 17149, 17090, 17548, 17474, 17564, 17530, 17237, 
17679, 17166, 17470, 17427, 17306, 17677, 17600, 17458, 17697, 
17602, 16990, 17111, 17150, 17561, 17406, 17135, 17181, 17014, 
17419, 17273, 17416, 17101, 17367, 17170, 17015, 17386, 17444, 
17507, 17592, 17058, 17292, 16966, 17756, 17239, 17479, 17260, 
17477, 16989, 17032, 17219, 17430, 17696, 17487, 17578, 17759, 
17269, 17634, 17279, 17478, 17222, 17296), class = "Date"), count = c(2, 
4, 2, 3, 6, 1, 4, 8, 1, 5, 1, 5, 1, 1, 2, 6, 3, 5, 2, 7, 3, 4, 
1, 3, 4, 2, 4, 1, 2, 3, 16, 1, 5, 4, 3, 4, 4, 6, 1, 3, 3, 1, 
3, 10, 5, 1, 4, 2, 2, 4, 5, 26, 4, 9, 3, 1, 3, 1, 4, 1, 2, 3, 
1, 13, 3, 1, 3, 1, 1, 3, 1, 3, 3, 4, 1, 2, 2, 3, 1, 9, 3, 1, 
2, 1, 4, 2, 1, 2, 4, 3, 2, 3, 1, 6, 5, 1, 2, 2, 3, 4), group = c("NON-FOOD", 
NA, NA, NA, NA, "MIX", NA, NA, "MIX", NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, "FOOD", NA, "FOOD", NA, NA, "MIX", 
NA, NA, NA, "FOOD", "FOOD", NA, NA, NA, NA, "FOOD", NA, NA, "FOOD", 
NA, NA, NA, "FOOD", NA, NA, NA, NA, NA, NA, NA, NA, "MIX", NA, 
NA, "FOOD", NA, "FOOD", NA, NA, "FOOD", NA, "FOOD", NA, NA, "NON-FOOD", 
NA, NA, "MIX", "NON-FOOD", NA, NA, NA, NA, NA, NA, "IMAGE", NA, 
"FOOD", NA, NA, NA, "FOOD", NA, "FOOD", NA, NA, NA, NA, NA, NA, 
NA, NA, "FOOD", "FOOD", NA, NA, NA)), row.names = c(73008L, 535553L, 
122359L, 321655L, 105632L, 646925L, 172409L, 256204L, 394666L, 
72385L, 20180L, 156162L, 478525L, 91409L, 485397L, 501386L, 277336L, 
100902L, 549629L, 640676L, 209400L, 16603L, 224543L, 272638L, 
505291L, 475497L, 131845L, 529041L, 558295L, 491746L, 67156L, 
23499L, 11150L, 334454L, 154958L, 150674L, 333348L, 45599L, 536064L, 
150673L, 20668L, 524095L, 151809L, 105713L, 433853L, 375687L, 
445626L, 420587L, 208594L, 562514L, 162403L, 372594L, 338509L, 
259784L, 560356L, 480072L, 361471L, 579474L, 481262L, 26469L, 
122119L, 152537L, 443426L, 325045L, 140531L, 171908L, 43547L, 
333968L, 237152L, 332106L, 114754L, 298081L, 164923L, 43577L, 
311250L, 350267L, 404348L, 470188L, 78329L, 250086L, 9486L, 638289L, 
209638L, 379370L, 227299L, 377487L, 26333L, 55058L, 195261L, 
340666L, 578515L, 387600L, 457752L, 640729L, 235389L, 514348L, 
240303L, 378836L, 197409L, 252746L), class = "data.frame")

1 个答案:

答案 0 :(得分:5)

这是执行函数前几步的一种方法(我没有深入研究所有ggplot的内容,只是介绍了如何进行分组的方法)。通常,要设置默认的“不执行任何操作”操作(例如默认为不分组),您将在函数中使用argument = NULL -您可以查看其他函数的文档页面以了解如何完成此操作。 Here's an SO post关于NANULL之间的区别。

我并不擅长使用quosure,但是我已经构建了一些功能,并且经常依赖于某些rlang / tidyselect辅助函数,例如我所使用的rlang::quo_is_null在这里使用。无需其他人,其他人也可以重写此代码。

首先查看您要查找的行为,以分组摘要或未分组摘要为例:

library(tidyverse)

# grouped
df %>%
  filter(!is.na(group)) %>%
  group_by(group) %>%
  summarise(count = sum(count, na.rm = TRUE))
#> # A tibble: 4 x 2
#>   group    count
#>   <chr>    <dbl>
#> 1 FOOD        34
#> 2 IMAGE        1
#> 3 MIX          8
#> 4 NON-FOOD     6

# not grouped
df %>%
  # add in if you want to filter ungrouped data
  summarise(count = sum(count, na.rm = TRUE))
#>   count
#> 1   347

然后在函数中,我创建what_var作为what的通用版本(语言专家,请随时用此术语纠正我...?)。通常,我会在名称中添加_var来跟踪原始参数是什么以及已经被enquo处理过的参数。通过创建by的等式并检查其是否为null,来检查参数by是否为null。如果不为空,即为by提供了一些列名,则根据该过滤条件进行过滤和分组。如果为null,则沿原始数据帧传递。我将数据传递给else语句中的新变量,以避免对原始数据帧进行操作。然后,无论数据是否分组,都汇总what

to_group_or_not_to_group <- function(data, what, by = NULL) {
  what_var <- enquo(what)

  if(!rlang::quo_is_null(enquo(by))) {
    by_var <- enquo(by)

    grouped_or_not <- data %>%
      filter(!is.na(!!by_var)) %>%
      group_by(!!by_var)
  } else {
    grouped_or_not <- data
  }

  grouped_or_not %>%
    summarise(!!quo_name(what_var) := sum(!!what_var, na.rm = TRUE))

}

验证您是否达到了目标结果。带有分组变量:

df %>%
  to_group_or_not_to_group(what = count, by = group)
#> # A tibble: 4 x 2
#>   group    count
#>   <chr>    <dbl>
#> 1 FOOD        34
#> 2 IMAGE        1
#> 3 MIX          8
#> 4 NON-FOOD     6

NULL提供分组变量(不存在):

df %>%
  to_group_or_not_to_group(what = count, by = NULL)
#>   count
#> 1   347

没有分组变量,请使用默认的by = NULL

df %>%
  to_group_or_not_to_group(what = count)
#>   count
#> 1   347

reprex package(v0.2.1)于2018-10-16创建