Skip to content

Commit

Permalink
Allow to customize label sep (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
yanlinlin82 committed Sep 4, 2020
1 parent ab5ce9d commit 85ac517
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 11 deletions.
6 changes: 5 additions & 1 deletion R/geom_venn.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#' @param columns A character vector use as index to select columns/elements.
#' @param set_names Set names, use column names if omitted.
#' @param show_percentage Show percentage for each set.
#' @param label_sep separator character for displaying elements.
#' @param fill_color Filling colors in circles.
#' @param fill_alpha Transparency for filling circles.
#' @param stroke_color Stroke color for drawing circles.
Expand Down Expand Up @@ -65,6 +66,7 @@ geom_venn <- function(mapping = NULL, data = NULL,
...,
set_names = NULL,
show_percentage = TRUE,
label_sep = ",",
fill_color = c("blue", "yellow", "green", "red"),
fill_alpha = .5,
stroke_color = "black",
Expand Down Expand Up @@ -92,6 +94,7 @@ geom_venn <- function(mapping = NULL, data = NULL,
self$geom$set_names <- set_names
}
self$geom$customize_attributes <- list(show_percentage = show_percentage,
label_sep = label_sep,
fill_color = fill_color,
fill_alpha = fill_alpha,
stroke_color = stroke_color,
Expand Down Expand Up @@ -123,7 +126,8 @@ GeomVenn <- ggproto("GeomVenn", Geom,
show_elements <- "label"
}
show_percentage <- attr$show_percentage
venn <- prepare_venn_data(data, sets, show_elements, show_percentage)
label_sep <- attr$label_sep
venn <- prepare_venn_data(data, sets, show_elements, show_percentage, label_sep)
d0 <- coord_munch(coord, venn$shapes, panel_params)
d <- d0 %>%
filter(!duplicated(group)) %>%
Expand Down
20 changes: 11 additions & 9 deletions R/ggvenn.R
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,9 @@ ggvenn <- function(data, columns = NULL,
set_name_color = "black",
set_name_size = 6,
text_color = "black",
text_size = 4) {
venn <- prepare_venn_data(data, columns, show_elements, show_percentage)
text_size = 4,
label_sep = ",") {
venn <- prepare_venn_data(data, columns, show_elements, show_percentage, label_sep)
venn$shapes %>%
mutate(group = LETTERS[group]) %>%
ggplot() +
Expand Down Expand Up @@ -171,7 +172,8 @@ gen_label_pos_4 <- function() {
}

prepare_venn_data <- function(data, columns = NULL,
show_elements = FALSE, show_percentage = TRUE) {
show_elements = FALSE, show_percentage = TRUE,
label_sep = ",") {
if (is.data.frame(data)) {
if (is.null(columns)) {
columns = data %>% select_if(is.logical) %>% names
Expand All @@ -194,7 +196,7 @@ prepare_venn_data <- function(data, columns = NULL,
(!xor(d1$B[[i]], as_tibble(data)[,columns[[2]]])))
d1$n[[i]] <- sum(idx)
if (!identical(show_elements, FALSE)) {
d1$text[[i]] <- paste(unlist(as_tibble(data)[idx,show_elements]), collapse = ",")
d1$text[[i]] <- paste(unlist(as_tibble(data)[idx,show_elements]), collapse = label_sep)
}
}
d2 <- gen_label_pos_2()
Expand All @@ -211,7 +213,7 @@ prepare_venn_data <- function(data, columns = NULL,
(!xor(d1$C[[i]], as_tibble(data)[,columns[[3]]])))
d1$n[[i]] <- sum(idx)
if (!identical(show_elements, FALSE)) {
d1$text[[i]] <- paste(unlist(as_tibble(data)[idx,show_elements]), collapse = ",")
d1$text[[i]] <- paste(unlist(as_tibble(data)[idx,show_elements]), collapse = label_sep)
}
}
d2 <- gen_label_pos_3()
Expand All @@ -230,7 +232,7 @@ prepare_venn_data <- function(data, columns = NULL,
(d1$D[[i]] == as_tibble(data)[,columns[[4]], drop = TRUE]))
d1$n[[i]] <- sum(idx)
if (!identical(show_elements, FALSE)) {
d1$text[[i]] <- paste(unlist(as_tibble(data)[idx,show_elements]), collapse = ",")
d1$text[[i]] <- paste(unlist(as_tibble(data)[idx,show_elements]), collapse = label_sep)
}
}
d2 <- gen_label_pos_4()
Expand All @@ -252,7 +254,7 @@ prepare_venn_data <- function(data, columns = NULL,
idx <- ((!xor(d1$A[[i]], a2 %in% data[[columns[[1]]]])) &
(!xor(d1$B[[i]], a2 %in% data[[columns[[2]]]])))
d1$n[[i]] <- sum(idx)
d1$text[[i]] <- paste(a2[idx], collapse = ",")
d1$text[[i]] <- paste(a2[idx], collapse = label_sep)
}
d2 <- gen_label_pos_2()
} else if (length(columns) == 3) {
Expand All @@ -264,7 +266,7 @@ prepare_venn_data <- function(data, columns = NULL,
(!xor(d1$B[[i]], a2 %in% data[[columns[[2]]]])) &
(!xor(d1$C[[i]], a2 %in% data[[columns[[3]]]])))
d1$n[[i]] <- sum(idx)
d1$text[[i]] <- paste(a2[idx], collapse = ",")
d1$text[[i]] <- paste(a2[idx], collapse = label_sep)
}
d2 <- gen_label_pos_3()
} else if (length(columns) == 4) {
Expand All @@ -277,7 +279,7 @@ prepare_venn_data <- function(data, columns = NULL,
(!xor(d1$C[[i]], a2 %in% data[[columns[[3]]]])) &
(!xor(d1$D[[i]], a2 %in% data[[columns[[4]]]])))
d1$n[[i]] <- sum(idx)
d1$text[[i]] <- paste(a2[idx], collapse = ",")
d1$text[[i]] <- paste(a2[idx], collapse = label_sep)
}
d2 <- gen_label_pos_4()
} else {
Expand Down
3 changes: 3 additions & 0 deletions man/geom_venn.Rd

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

3 changes: 2 additions & 1 deletion man/ggvenn.Rd

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

0 comments on commit 85ac517

Please sign in to comment.