Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Introduce show_stats argument for enhanced control over displayed statistics #46

Merged
merged 5 commits into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: ggvenn
Title: Draw Venn Diagram by 'ggplot2'
Version: 0.1.13
Version: 0.1.14
Authors@R: person(given = "Linlin", family = "Yan",
role = c("aut", "cre"),
email = "[email protected]",
Expand All @@ -18,3 +18,6 @@ Imports: dplyr, grid, scales
Suggests:
testthat (>= 3.0.0)
Config/testthat/edition: 3
Roxygen: list(markdown = TRUE)
Imports:
lifecycle
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,5 @@ importFrom(grid,gpar)
importFrom(grid,grobTree)
importFrom(grid,polygonGrob)
importFrom(grid,textGrob)
importFrom(lifecycle,deprecated)
importFrom(stats,na.omit)
15 changes: 9 additions & 6 deletions R/geom_venn.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@

#' @param data A data.frame or a list as input data.
#' @param set_names Set names, use column names if omitted.
#' @param show_percentage Show percentage for each set.
#' @param show_stats Show count (c) and/or percentage (p) for each set.
#' Pass a string like "cp" to show both.
#' @param show_percentage Show percentage for each set. Deprecated, use show_stats instead.
#' @param digits The desired number of digits after the decimal point
#' @param label_sep separator character for displaying elements.
#' @param count_column Specify column for element repeat count.
Expand Down Expand Up @@ -55,7 +57,7 @@
#'
#' # hide percentage
#' ggplot(d) +
#' geom_venn(aes(A = `Set 1`, B = `Set 2`), show_percentage = FALSE) +
#' geom_venn(aes(A = `Set 1`, B = `Set 2`), show_stats = 'c') +
#' coord_fixed() +
#' theme_void()
#'
Expand All @@ -77,7 +79,8 @@ geom_venn <- function(mapping = NULL, data = NULL,
stat = "identity", position = "identity",
...,
set_names = NULL,
show_percentage = TRUE,
show_stats = 'cp',
show_percentage = deprecated(),
digits = 1,
label_sep = ",",
count_column = NULL,
Expand Down Expand Up @@ -110,7 +113,7 @@ geom_venn <- function(mapping = NULL, data = NULL,
} else {
self$geom$set_names <- set_names
}
self$geom$customize_attributes <- list(show_percentage = show_percentage,
self$geom$customize_attributes <- list(show_stats = show_stats,
digits = digits,
label_sep = label_sep,
count_column = count_column,
Expand Down Expand Up @@ -147,13 +150,13 @@ GeomVenn <- ggproto("GeomVenn", Geom,
if ("label" %in% names(data)) {
show_elements <- "label"
}
show_percentage <- attr$show_percentage
show_stats <- attr$show_stats
digits <- attr$digits
label_sep <- attr$label_sep
count_column <- attr$count_column
show_outside <- attr$show_outside
auto_scale <- attr$auto_scale
venn <- prepare_venn_data(data, sets, show_elements, show_percentage, digits,
venn <- prepare_venn_data(data, sets, show_elements, show_stats, digits,
label_sep, count_column, show_outside, auto_scale)
d0 <- coord_munch(coord, venn$shapes, panel_params)
d <- d0 %>%
Expand Down
7 changes: 7 additions & 0 deletions R/ggvenn-package.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#' @keywords internal
"_PACKAGE"

## usethis namespace: start
#' @importFrom lifecycle deprecated
## usethis namespace: end
NULL
63 changes: 44 additions & 19 deletions R/ggvenn.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
#' @param data A data.frame or a list as input data.
#' @param columns A character vector use as index to select columns/elements.
#' @param show_elements Show set elements instead of count/percentage.
#' @param show_percentage Show percentage for each set.
#' @param show_stats Show count (c) and/or percentage (p) for each set.
#' Pass a string like "cp" to show both.
#' @param show_percentage Show percentage for each set. Deprecated, use show_stats instead.
#' @param digits The desired number of digits after the decimal point
#' @param fill_color Filling colors in circles.
#' @param fill_alpha Transparency for filling circles.
Expand Down Expand Up @@ -48,7 +50,7 @@
#' ggvenn(d, c("Set 1", "Set 2"), fill_color = c("red", "blue"))
#'
#' # hide percentage
#' ggvenn(d, c("Set 1", "Set 2"), show_percentage = FALSE)
#' ggvenn(d, c("Set 1", "Set 2"), show_stats = 'c')
#'
#' # change precision of percentages
#' ggvenn(d, c("Set 1", "Set 2"), digits = 2)
Expand All @@ -63,7 +65,8 @@
#' @export
ggvenn <- function(data, columns = NULL,
show_elements = FALSE,
show_percentage = TRUE,
show_stats = 'cp',
show_percentage = lifecycle::deprecated(),
digits = 1,
fill_color = c("blue", "yellow", "green", "red"),
fill_alpha = .5,
Expand All @@ -81,7 +84,12 @@ ggvenn <- function(data, columns = NULL,
auto_scale = FALSE,
comma_sep=FALSE) {
show_outside <- match.arg(show_outside)
venn <- prepare_venn_data(data, columns, show_elements, show_percentage, digits,
if (lifecycle::is_present(show_percentage)) {
lifecycle::deprecate_soft("0.1.11", "ggvenn::ggvenn(show_percentage = )", "ggvenn::ggvenn(show_stats = )")

show_stats <- if (show_percentage) "cp" else "c"
}
venn <- prepare_venn_data(data, columns, show_elements, show_stats, digits,
label_sep, count_column, show_outside, auto_scale,
comma_sep=comma_sep)
g <- venn$shapes %>%
Expand Down Expand Up @@ -380,7 +388,7 @@ gen_label_pos_4 <- function() {
}

prepare_venn_data <- function(data, columns = NULL,
show_elements = FALSE, show_percentage = TRUE, digits = 1,
show_elements = FALSE, show_stats = "cp", digits = 1,
label_sep = ",", count_column = NULL,
show_outside = c("auto", "none", "always"),
auto_scale = FALSE, comma_sep=FALSE) {
Expand Down Expand Up @@ -534,20 +542,37 @@ prepare_venn_data <- function(data, columns = NULL,
df_text <- df_text[-nrow(df_text), ]
}
if (!show_elements) {
fmt <- sprintf("%%d\n(%%.%df%%%%)", digits)
if (show_percentage) {
if(comma_sep) {
fmt <- sprintf("%%s\n(%%.%df%%%%)", digits)
df_text <- df_text %>% mutate(text = sprintf(fmt,
scales::label_comma()(n), 100 * n / sum(n)))
}else
df_text <- df_text %>% mutate(text = sprintf(fmt, n, 100 * n / sum(n)))
} else {
if(comma_sep){
df_text <- df_text %>% mutate(text = sprintf("%s", scales::label_comma()(n)))
}else
df_text <- df_text %>% mutate(text = sprintf("%d", n))
fmt_count <- "%d"
fmt_percentage <- sprintf("%%.%df%%%%", digits)
fmt_both <- sprintf("%%d\n(%%.%df%%%%)", digits)

if (comma_sep) {
fmt_count <- "%s"
fmt_percentage <- sprintf("%%.%df%%%%", digits)
fmt_both <- sprintf("%%s\n(%%.%df%%%%)", digits)
}

total_count <- sum(df_text$n)

df_text <- df_text %>% mutate(text = dplyr::case_when(
show_stats == "c" ~ {
if (comma_sep) {
sprintf(fmt_count, scales::label_comma()(n))
} else {
sprintf(fmt_count, n)
}
},
show_stats == "p" ~ sprintf(fmt_percentage, 100 * n / total_count),
show_stats == "cp" ~ {
if (comma_sep) {
sprintf(fmt_both, scales::label_comma()(n), 100 * n / total_count)
} else {
sprintf(fmt_both, n, 100 * n / total_count)
}
},
TRUE ~ ""
))

}
}
list(shapes = df_shape, texts = df_text, labels = df_label, segs = df_seg)
}
10 changes: 7 additions & 3 deletions man/geom_venn.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions man/ggvenn-package.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 7 additions & 3 deletions man/ggvenn.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions tests/testthat/_snaps/percentages.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
Code
ggplot_build(ggvenn(a, show_percentage = FALSE))$data
Warning <lifecycle_warning_deprecated>
The `show_percentage` argument of `ggvenn()` is deprecated as of ggvenn 0.1.11.
i Please use the `show_stats` argument instead.
Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
i Please use `linewidth` instead.
Output
Expand Down Expand Up @@ -637,6 +639,8 @@
Code
ggplot_build(ggvenn(a, show_percentage = TRUE))$data
Warning <lifecycle_warning_deprecated>
The `show_percentage` argument of `ggvenn()` is deprecated as of ggvenn 0.1.11.
i Please use the `show_stats` argument instead.
Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
i Please use `linewidth` instead.
Output
Expand Down Expand Up @@ -1271,6 +1275,8 @@
Code
ggplot_build(ggvenn(a, show_percentage = TRUE, digits = 2))$data
Warning <lifecycle_warning_deprecated>
The `show_percentage` argument of `ggvenn()` is deprecated as of ggvenn 0.1.11.
i Please use the `show_stats` argument instead.
Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
i Please use `linewidth` instead.
Output
Expand Down
Loading