Skip to content

Commit

Permalink
Merge pull request #6 from dhammarstrom/value_type-suggestion
Browse files Browse the repository at this point in the history
Added value_type argument to choose what value to display
  • Loading branch information
yanlinlin82 authored Mar 13, 2020
2 parents 086ce40 + 4bfed08 commit 8961545
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 88 deletions.
2 changes: 2 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
^.*\.Rproj$
^\.Rproj\.user$
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.Rproj.user
.Rhistory
.RData
.Ruserdata
114 changes: 57 additions & 57 deletions R/geom_venn.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
#'
#' # use data.frame as input
#' d <- tibble(value = c(1, 2, 3, 5, 6, 7, 8, 9, 10, 12, 13),
#' `Set 1` = c(T, F, T, T, F, T, F, T, F, F, F),
#' `Set 2` = c(T, F, F, T, F, F, F, T, F, F, T),
#' `Set 3` = c(T, T, F, F, F, F, T, T, F, F, F),
#' `Set 4` = c(F, F, F, F, T, T, F, F, T, T, F))
#' `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 Down Expand Up @@ -68,8 +68,8 @@ geom_venn <- function(mapping = NULL, data = NULL,
text_color = "black",
text_size = 4) {
l <- layer(mapping = mapping, data = data,
geom = GeomVenn, stat = stat, position = position,
params = list(na.rm = TRUE, ...))
geom = GeomVenn, stat = stat, position = position,
params = list(na.rm = TRUE, ...))
old_compute_aesthetics <- l$compute_aesthetics
l$compute_aesthetics <- function(self, data, plot) {
if (is.null(set_names)) {
Expand Down Expand Up @@ -99,55 +99,55 @@ geom_venn <- function(mapping = NULL, data = NULL,
}

GeomVenn <- ggproto("GeomVenn", Geom,
required_aes = c("A", "B"),
optional_aes = c("C", "D", "label"),
extra_params = c("na.rm"),
setup_data = function(self, data, params) {
data %>% mutate(xmin = -2, xmax = 2, ymin = -2, ymax = 2)
},
draw_panel = function(self, data, panel_params, coord, ...) {
attr <- self$customize_attributes
sets <- c("A", "B", "C", "D")
sets <- sets[sets %in% names(data)]
show_elements <- NA
if ("label" %in% names(data)) {
show_elements <- "label"
}
venn <- prepare_venn_data(data, sets, show_elements)
d0 <- coord_munch(coord, venn$shapes, panel_params)
d <- d0 %>%
filter(!duplicated(group)) %>%
mutate(fill_color = attr$fill_color[group],
fill_alpha = attr$fill_alpha,
stroke_color = attr$stroke_color,
stroke_alpha = attr$stroke_alpha,
stroke_size = attr$stroke_size,
stroke_linetype = attr$stroke_linetype)
d1 <- coord_munch(coord, venn$labels, panel_params)
d2 <- coord_munch(coord, venn$texts, panel_params)
ggplot2:::ggname("geom_venn",
grobTree(
polygonGrob(
d0$x, d0$y, default.units = "native", id = d0$group,
gp = gpar(col = NA,
fill = alpha(d$fill_color, d$fill_alpha))),
polygonGrob(
d0$x, d0$y, default.units = "native", id = d0$group,
gp = gpar(col = alpha(d$stroke_color, d$stroke_alpha),
fill = NA,
lwd = d$stroke_size * .pt,
lty = d$stroke_linetype)),
textGrob(
self$set_names, d1$x, d1$y, default.units = "native",
hjust = d1$hjust, vjust = d1$vjust,
gp = gpar(col = attr$set_name_color,
fontsize = attr$set_name_size * .pt)),
textGrob(
d2$text, d2$x, d2$y, default.units = "native",
hjust = d2$hjust, vjust = d2$vjust,
gp = gpar(col = attr$text_color,
fontsize = attr$text_size * .pt))
)
)
}
required_aes = c("A", "B"),
optional_aes = c("C", "D", "label"),
extra_params = c("na.rm"),
setup_data = function(self, data, params) {
data %>% mutate(xmin = -2, xmax = 2, ymin = -2, ymax = 2)
},
draw_panel = function(self, data, panel_params, coord, ...) {
attr <- self$customize_attributes
sets <- c("A", "B", "C", "D")
sets <- sets[sets %in% names(data)]
show_elements <- NA
if ("label" %in% names(data)) {
show_elements <- "label"
}
venn <- prepare_venn_data(data, sets, show_elements)
d0 <- coord_munch(coord, venn$shapes, panel_params)
d <- d0 %>%
filter(!duplicated(group)) %>%
mutate(fill_color = attr$fill_color[group],
fill_alpha = attr$fill_alpha,
stroke_color = attr$stroke_color,
stroke_alpha = attr$stroke_alpha,
stroke_size = attr$stroke_size,
stroke_linetype = attr$stroke_linetype)
d1 <- coord_munch(coord, venn$labels, panel_params)
d2 <- coord_munch(coord, venn$texts, panel_params)
ggplot2:::ggname("geom_venn",
grobTree(
polygonGrob(
d0$x, d0$y, default.units = "native", id = d0$group,
gp = gpar(col = NA,
fill = alpha(d$fill_color, d$fill_alpha))),
polygonGrob(
d0$x, d0$y, default.units = "native", id = d0$group,
gp = gpar(col = alpha(d$stroke_color, d$stroke_alpha),
fill = NA,
lwd = d$stroke_size * .pt,
lty = d$stroke_linetype)),
textGrob(
self$set_names, d1$x, d1$y, default.units = "native",
hjust = d1$hjust, vjust = d1$vjust,
gp = gpar(col = attr$set_name_color,
fontsize = attr$set_name_size * .pt)),
textGrob(
d2$text, d2$x, d2$y, default.units = "native",
hjust = d2$hjust, vjust = d2$vjust,
gp = gpar(col = attr$text_color,
fontsize = attr$text_size * .pt))
)
)
}
)
58 changes: 35 additions & 23 deletions R/ggvenn.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +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 fill_color Filling colors in circles.
#' @param fill_alpha Transparency for filling circles.
#' @param stroke_color Stroke color for drawing circles.
Expand All @@ -29,10 +30,10 @@
#'
#' # use data.frame as input
#' d <- tibble(value = c(1, 2, 3, 5, 6, 7, 8, 9, 10, 12, 13),
#' `Set 1` = c(T, F, T, T, F, T, F, T, F, F, F),
#' `Set 2` = c(T, F, F, T, F, F, F, T, F, F, T),
#' `Set 3` = c(T, T, F, F, F, F, T, T, F, F, F),
#' `Set 4` = c(F, F, F, F, T, T, F, F, T, T, F))
#' `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)
Expand All @@ -47,6 +48,7 @@
#' @export
ggvenn <- function(data, columns = NULL,
show_elements = FALSE,
value_type = "both",
fill_color = c("blue", "yellow", "green", "red"),
fill_alpha = .5,
stroke_color = "black",
Expand All @@ -57,7 +59,7 @@ ggvenn <- function(data, columns = NULL,
set_name_size = 6,
text_color = "black",
text_size = 4) {
venn <- prepare_venn_data(data, columns, show_elements)
venn <- prepare_venn_data(data, columns, show_elements, value_type)
venn$shapes %>%
mutate(group = LETTERS[group]) %>%
ggplot() +
Expand Down Expand Up @@ -88,10 +90,10 @@ gen_circle <- function(group, x_offset = 0, y_offset = 0, radius = 1,
radius_b = radius, theta_offset = 0, length.out = 100) {
tibble(group = group,
theta = seq(0, 2 * pi, length.out = length.out)) %>%
mutate(x_raw = radius * cos(theta),
y_raw = radius_b * sin(theta),
x = x_offset + x_raw * cos(theta_offset) - y_raw * sin(theta_offset),
y = y_offset + x_raw * sin(theta_offset) + y_raw * cos(theta_offset))
mutate(x_raw = radius * cos(theta),
y_raw = radius_b * sin(theta),
x = x_offset + x_raw * cos(theta_offset) - y_raw * sin(theta_offset),
y = y_offset + x_raw * sin(theta_offset) + y_raw * cos(theta_offset))
}

gen_circle_2 <- function() {
Expand Down Expand Up @@ -164,7 +166,7 @@ gen_label_pos_4 <- function() {
"D", 1.5, -1.3, 0, 1)
}

prepare_venn_data <- function(data, columns = NULL, show_elements = FALSE) {
prepare_venn_data <- function(data, columns = NULL, show_elements = FALSE, value_type = "both") {
if (is.data.frame(data)) {
if (is.null(columns)) {
columns = data %>% select_if(is.logical) %>% names
Expand All @@ -184,7 +186,7 @@ prepare_venn_data <- function(data, columns = NULL, show_elements = FALSE) {
stopifnot((d1 %>% count(A, B) %>% with(n)) == 1)
for (i in 1:nrow(d1)) {
idx <- ((!xor(d1$A[[i]], as_tibble(data)[,columns[[1]]])) &
(!xor(d1$B[[i]], as_tibble(data)[,columns[[2]]])))
(!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 = ",")
Expand All @@ -200,8 +202,8 @@ prepare_venn_data <- function(data, columns = NULL, show_elements = FALSE) {
stopifnot((d1 %>% count(A, B, C) %>% with(n)) == 1)
for (i in 1:nrow(d1)) {
idx <- ((!xor(d1$A[[i]], as_tibble(data)[,columns[[1]]])) &
(!xor(d1$B[[i]], as_tibble(data)[,columns[[2]]])) &
(!xor(d1$C[[i]], as_tibble(data)[,columns[[3]]])))
(!xor(d1$B[[i]], as_tibble(data)[,columns[[2]]])) &
(!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 = ",")
Expand All @@ -218,9 +220,9 @@ prepare_venn_data <- function(data, columns = NULL, show_elements = FALSE) {
stopifnot((d1 %>% count(A, B, C, D) %>% with(n)) == 1)
for (i in 1:nrow(d1)) {
idx <- ((d1$A[[i]] == as_tibble(data)[,columns[[1]], drop = TRUE]) &
(d1$B[[i]] == as_tibble(data)[,columns[[2]], drop = TRUE]) &
(d1$C[[i]] == as_tibble(data)[,columns[[3]], drop = TRUE]) &
(d1$D[[i]] == as_tibble(data)[,columns[[4]], drop = TRUE]))
(d1$B[[i]] == as_tibble(data)[,columns[[2]], drop = TRUE]) &
(d1$C[[i]] == as_tibble(data)[,columns[[3]], drop = TRUE]) &
(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 = ",")
Expand All @@ -243,7 +245,7 @@ prepare_venn_data <- function(data, columns = NULL, show_elements = FALSE) {
stopifnot((d1 %>% count(A, B) %>% with(n)) == 1)
for (i in 1:nrow(d1)) {
idx <- ((!xor(d1$A[[i]], a2 %in% data[[columns[[1]]]])) &
(!xor(d1$B[[i]], a2 %in% data[[columns[[2]]]])))
(!xor(d1$B[[i]], a2 %in% data[[columns[[2]]]])))
d1$n[[i]] <- sum(idx)
d1$text[[i]] <- paste(a2[idx], collapse = ",")
}
Expand All @@ -254,8 +256,8 @@ prepare_venn_data <- function(data, columns = NULL, show_elements = FALSE) {
stopifnot((d1 %>% count(A, B, C) %>% with(n)) == 1)
for (i in 1:nrow(d1)) {
idx <- ((!xor(d1$A[[i]], a2 %in% data[[columns[[1]]]])) &
(!xor(d1$B[[i]], a2 %in% data[[columns[[2]]]])) &
(!xor(d1$C[[i]], a2 %in% data[[columns[[3]]]])))
(!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 = ",")
}
Expand All @@ -266,9 +268,9 @@ prepare_venn_data <- function(data, columns = NULL, show_elements = FALSE) {
stopifnot((d1 %>% count(A, B, C, D) %>% with(n)) == 1)
for (i in 1:nrow(d1)) {
idx <- ((!xor(d1$A[[i]], a2 %in% data[[columns[[1]]]])) &
(!xor(d1$B[[i]], a2 %in% data[[columns[[2]]]])) &
(!xor(d1$C[[i]], a2 %in% data[[columns[[3]]]])) &
(!xor(d1$D[[i]], a2 %in% data[[columns[[4]]]])))
(!xor(d1$B[[i]], a2 %in% data[[columns[[2]]]])) &
(!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 = ",")
}
Expand All @@ -281,7 +283,17 @@ prepare_venn_data <- function(data, columns = NULL, show_elements = FALSE) {
stop("`data` should be a list")
}
if (!show_elements) {
d1 <- d1 %>% mutate(text = sprintf("%d\n(%.1f%%)", n, 100 * n / sum(n)))

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


}


list(shapes = d, texts = d1, labels = d2)
}
17 changes: 17 additions & 0 deletions ggvenn.Rproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Version: 1.0

RestoreWorkspace: Default
SaveWorkspace: Default
AlwaysSaveHistory: Default

EnableCodeIndexing: Yes
UseSpacesForTab: Yes
NumSpacesForTab: 2
Encoding: UTF-8

RnwWeave: Sweave
LaTeX: pdfLaTeX

BuildType: Package
PackageUseDevtools: Yes
PackageInstallArgs: --no-multiarch --with-keep.source
8 changes: 4 additions & 4 deletions man/geom_venn.Rd

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

11 changes: 7 additions & 4 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 8961545

Please sign in to comment.