R - dplyr 函数,仅在提供参数时具有可选分组



我需要编写一个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")

这是执行函数前几步的一种方法(我没有深入讨论所有ggplot内容,只是您如何处理分组(。通常,要设置默认的"不执行任何操作"操作,例如默认为不分组,您将在函数中使用argument = NULL- 您可以环顾其他函数的文档页面以了解这是如何完成的。这是一篇关于NANULL之间区别的SO帖子。

我不是很擅长使用仲裁,但是我已经构建了一些函数,并且经常依赖于一些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的商数版本(rlang 专家,请随时纠正我这个术语......?我通常会在名称中添加_var,以跟踪原始参数和已经enquo的内容。通过创建by的 quosure 并检查该参数是否为 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

创建于 2018-10-16 由 reprex 软件包 (v0.2.1(

相关内容

  • 没有找到相关文章

最新更新