Skip to content

Commit

Permalink
Merge pull request #42 from fkgruber/master
Browse files Browse the repository at this point in the history
Added option to add comma as separation for large numbers
  • Loading branch information
yanlinlin82 authored Apr 29, 2024
2 parents 69c36ce + 01f26ff commit 623b194
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Authors@R: person(given = "Linlin", family = "Yan",
Author: Linlin Yan [aut, cre] (<https://orcid.org/0000-0002-4990-6239>)
Maintainer: Linlin Yan <[email protected]>
Description: An easy-to-use way to draw pretty venn diagram by 'ggplot2'.
Depends: dplyr, grid, ggplot2
Depends: dplyr, grid, ggplot2, scales
URL: https://yanlinlin82.github.io/ggvenn/, https://github.com/yanlinlin82/ggvenn
BugReports: https://github.com/yanlinlin82/ggvenn/issues
License: MIT + file LICENSE
Expand Down
24 changes: 17 additions & 7 deletions R/ggvenn.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#' @param count_column Specify column for element repeat count.
#' @param show_outside Show outside elements (not belongs to any set).
#' @param auto_scale Allow automatically resizing circles according to element counts.
#' @param comma_sep Whether to use comma as separator for displaying numbers.
#' @return The ggplot object to print or save to file.
#' @examples
#' library(ggvenn)
Expand Down Expand Up @@ -77,10 +78,12 @@ ggvenn <- function(data, columns = NULL,
label_sep = ",",
count_column = NULL,
show_outside = c("auto", "none", "always"),
auto_scale = FALSE) {
auto_scale = FALSE,
comma_sep=FALSE) {
show_outside <- match.arg(show_outside)
venn <- prepare_venn_data(data, columns, show_elements, show_percentage, digits,
label_sep, count_column, show_outside, auto_scale)
label_sep, count_column, show_outside, auto_scale,
comma_sep=comma_sep)
g <- venn$shapes %>%
mutate(group = LETTERS[group]) %>%
ggplot() +
Expand Down Expand Up @@ -380,7 +383,7 @@ prepare_venn_data <- function(data, columns = NULL,
show_elements = FALSE, show_percentage = TRUE, digits = 1,
label_sep = ",", count_column = NULL,
show_outside = c("auto", "none", "always"),
auto_scale = FALSE) {
auto_scale = FALSE, comma_sep=FALSE) {
show_outside <- match.arg(show_outside)
if (is.data.frame(data)) {
if (is.null(columns)) {
Expand All @@ -392,7 +395,7 @@ prepare_venn_data <- function(data, columns = NULL,
show_elements <- show_elements[[1]]
show_elements %in% names(data)
} else { FALSE }}) {
stop("Value ", deparse(show_elements),
stop("Value ", deparse(show_elements),
" in `show_elements` does not correspond to any column name of the data frame.",
call. = FALSE)
}
Expand Down Expand Up @@ -533,11 +536,18 @@ prepare_venn_data <- function(data, columns = NULL,
if (!show_elements) {
fmt <- sprintf("%%d\n(%%.%df%%%%)", digits)
if (show_percentage) {
df_text <- df_text %>% mutate(text = sprintf(fmt, n, 100 * n / sum(n)))
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 {
df_text <- df_text %>% mutate(text = sprintf("%d", n))
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))
}
}
list(shapes = df_shape, texts = df_text, labels = df_label, segs = df_seg)
}

0 comments on commit 623b194

Please sign in to comment.