Skip to content

Commit

Permalink
Use option 'show_percentage' instead of 'value_type'
Browse files Browse the repository at this point in the history
  • Loading branch information
yanlinlin82 committed Mar 13, 2020
1 parent 8961545 commit 580e017
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 37 deletions.
26 changes: 18 additions & 8 deletions R/geom_venn.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#' @param data A data.frame or a list as input data.
#' @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 fill_color Filling colors in circles.
#' @param fill_alpha Transparency for filling circles.
#' @param stroke_color Stroke color for drawing circles.
Expand All @@ -20,11 +21,11 @@
#' library(ggvenn)
#'
#' # use data.frame as input
#' d <- tibble(value = c(1, 2, 3, 5, 6, 7, 8, 9, 10, 12, 13),
#' `Set 1` = c(TRUE, FALSE, TRUE, TRUE, FALSE, TRUE, FALSE, TRUE, FALSE, FALSE, FALSE),
#' `Set 2` = c(TRUE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE, TRUE, FALSE, FALSE, TRUE),
#' `Set 3` = c(TRUE, TRUE, FALSE, FALSE, FALSE, FALSE, TRUE, TRUE, FALSE, FALSE, FALSE),
#' `Set 4` = c(FALSE, FALSE, FALSE, FALSE, TRUE, TRUE, FALSE, FALSE, TRUE, TRUE, FALSE))
#' d <- tibble(value = c(1, 2, 3, 5, 6, 7, 8, 9, 10, 12, 13),
#' `Set 1` = c(TRUE, FALSE, TRUE, TRUE, FALSE, TRUE, FALSE, TRUE, FALSE, FALSE, FALSE),
#' `Set 2` = c(TRUE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE, TRUE, FALSE, FALSE, TRUE),
#' `Set 3` = c(TRUE, TRUE, FALSE, FALSE, FALSE, FALSE, TRUE, TRUE, FALSE, FALSE, FALSE),
#' `Set 4` = c(FALSE, FALSE, FALSE, FALSE, TRUE, TRUE, FALSE, FALSE, TRUE, TRUE, FALSE))
#'
#' # ggplot gramma
#' ggplot(d) +
Expand All @@ -46,6 +47,12 @@
#' coord_fixed() +
#' theme_void()
#'
#'#' # hide percentage
#' ggplot(d) +
#' geom_venn(aes(A = `Set 1`, B = `Set 2`), show_percentage = FALSE) +
#' coord_fixed() +
#' theme_void()
#'
#' # show elements instead of count/percentage
#' ggplot(d) +
#' geom_venn(aes(A = `Set 1`, B = `Set 2`, C = `Set 3`, D = `Set 4`, label = value)) +
Expand All @@ -57,6 +64,7 @@ geom_venn <- function(mapping = NULL, data = NULL,
stat = "identity", position = "identity",
...,
set_names = NULL,
show_percentage = TRUE,
fill_color = c("blue", "yellow", "green", "red"),
fill_alpha = .5,
stroke_color = "black",
Expand All @@ -83,7 +91,8 @@ geom_venn <- function(mapping = NULL, data = NULL,
} else {
self$geom$set_names <- set_names
}
self$geom$customize_attributes <- list(fill_color = fill_color,
self$geom$customize_attributes <- list(show_percentage = show_percentage,
fill_color = fill_color,
fill_alpha = fill_alpha,
stroke_color = stroke_color,
stroke_alpha = stroke_alpha,
Expand All @@ -109,11 +118,12 @@ GeomVenn <- ggproto("GeomVenn", Geom,
attr <- self$customize_attributes
sets <- c("A", "B", "C", "D")
sets <- sets[sets %in% names(data)]
show_elements <- NA
show_elements <- FALSE
if ("label" %in% names(data)) {
show_elements <- "label"
}
venn <- prepare_venn_data(data, sets, show_elements)
show_percentage <- attr$show_percentage
venn <- prepare_venn_data(data, sets, show_elements, show_percentage)
d0 <- coord_munch(coord, venn$shapes, panel_params)
d <- d0 %>%
filter(!duplicated(group)) %>%
Expand Down
32 changes: 15 additions & 17 deletions R/ggvenn.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#' @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 value_type Display "count" data only or "both" counts and percentages
#' @param show_percentage Show percentage for each set.
#' @param fill_color Filling colors in circles.
#' @param fill_alpha Transparency for filling circles.
#' @param stroke_color Stroke color for drawing circles.
Expand All @@ -29,26 +29,29 @@
#' ggvenn(a)
#'
#' # use data.frame as input
#' d <- tibble(value = c(1, 2, 3, 5, 6, 7, 8, 9, 10, 12, 13),
#' `Set 1` = c(TRUE, FALSE, TRUE, TRUE, FALSE, TRUE, FALSE, TRUE, FALSE, FALSE, FALSE),
#' `Set 2` = c(TRUE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE, TRUE, FALSE, FALSE, TRUE),
#' `Set 3` = c(TRUE, TRUE, FALSE, FALSE, FALSE, FALSE, TRUE, TRUE, FALSE, FALSE, FALSE),
#' `Set 4` = c(FALSE, FALSE, FALSE, FALSE, TRUE, TRUE, FALSE, FALSE, TRUE, TRUE, FALSE))
#' d <- tibble(value = c(1, 2, 3, 5, 6, 7, 8, 9, 10, 12, 13),
#' `Set 1` = c(TRUE, FALSE, TRUE, TRUE, FALSE, TRUE, FALSE, TRUE, FALSE, FALSE, FALSE),
#' `Set 2` = c(TRUE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE, TRUE, FALSE, FALSE, TRUE),
#' `Set 3` = c(TRUE, TRUE, FALSE, FALSE, FALSE, FALSE, TRUE, TRUE, FALSE, FALSE, FALSE),
#' `Set 4` = c(FALSE, FALSE, FALSE, FALSE, TRUE, TRUE, FALSE, FALSE, TRUE, TRUE, FALSE))
#' ggvenn(d, c("Set 1", "Set 2"))
#' ggvenn(d, c("Set 1", "Set 2", "Set 3"))
#' ggvenn(d)
#'
#' # set fill color
#' ggvenn(d, c("Set 1", "Set 2"), fill_color = c("red", "blue"))
#'
#' # hide percentage
#' ggvenn(d, c("Set 1", "Set 2"), show_percentage = FALSE)
#'
#' # show elements instead of count/percentage
#' ggvenn(a, show_elements = TRUE)
#' ggvenn(d, show_elements = "value")
#' @seealso geom_venn
#' @export
ggvenn <- function(data, columns = NULL,
show_elements = FALSE,
value_type = "both",
show_percentage = TRUE,
fill_color = c("blue", "yellow", "green", "red"),
fill_alpha = .5,
stroke_color = "black",
Expand All @@ -59,7 +62,7 @@ ggvenn <- function(data, columns = NULL,
set_name_size = 6,
text_color = "black",
text_size = 4) {
venn <- prepare_venn_data(data, columns, show_elements, value_type)
venn <- prepare_venn_data(data, columns, show_elements, show_percentage)
venn$shapes %>%
mutate(group = LETTERS[group]) %>%
ggplot() +
Expand Down Expand Up @@ -166,7 +169,8 @@ gen_label_pos_4 <- function() {
"D", 1.5, -1.3, 0, 1)
}

prepare_venn_data <- function(data, columns = NULL, show_elements = FALSE, value_type = "both") {
prepare_venn_data <- function(data, columns = NULL,
show_elements = FALSE, show_percentage = TRUE) {
if (is.data.frame(data)) {
if (is.null(columns)) {
columns = data %>% select_if(is.logical) %>% names
Expand Down Expand Up @@ -283,17 +287,11 @@ prepare_venn_data <- function(data, columns = NULL, show_elements = FALSE, value
stop("`data` should be a list")
}
if (!show_elements) {

if(value_type == "both"){
if (show_percentage) {
d1 <- d1 %>% mutate(text = sprintf("%d\n(%.1f%%)", n, 100 * n / sum(n)))
}
if(value_type == "count"){
} else {
d1 <- d1 %>% mutate(text = sprintf("%d", n, 100 * n / sum(n)))
}


}


list(shapes = d, texts = d1, labels = d2)
}
19 changes: 14 additions & 5 deletions man/geom_venn.Rd

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

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

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

0 comments on commit 580e017

Please sign in to comment.