knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.width = 5, fig.height = (5*5.5)/6, out.width = "90%", dpi = 150 )
library(ggplot2) library(ggvd)
This package aims to simplify making 2-4 way Venn diagrams with ggplot2, whilst allowing for easy customisation. It builds upon the ggvenn and ggVennDiagram packages but is (in my opinion) simpler use. In future I hope to adapt the 2-3 way Euler diagrams from VennDiagram as well.
For comparing 5 or more sets, it is better to use UpSet plots (see ggupset, UpSetR or ComplexUpset).
prepare_venn()
Starting with a list of numeric or characters vectors (i.e. a list of sets) to
compare, you must construct a data.frame of the appropriate format using
prepare_venn()
. It is important that within each vector in the list, there
are no duplicated elements.
# Create dummy data set.seed(42) lst <- list( "Set 1" = sample.int(2000, size = 1651, replace = FALSE), "Set 2" = sample.int(2000, size = 687, replace = FALSE), "Set 3" = sample.int(2000, size = 872, replace = FALSE), "Set 4" = sample.int(2000, size = 712, replace = FALSE) )
# Ensure no duplicates within each set, # otherwise prepare_venn() will produce an error sapply(lst, function(x) any(duplicated(x)))
# Create a data.frame to input into ggplot() venn4_df <- prepare_venn(lst) venn4_df
# Add a column for discrete fill colours venn4_df <- prepare_venn(lst, fill = c("blue", "yellow", "green", "red")) venn4_df
# Create data.frames for 2 and 3-way Venns as well venn2_df <- prepare_venn(lst[1:2]) venn3_df <- prepare_venn(lst[1:3])
geom_venn()
# The most basic outputs ggplot() + geom_venn(aes(set_name = set_name, elements = elements), data = venn2_df) ggplot() + geom_venn(aes(set_name = set_name, elements = elements), data = venn3_df) ggplot() + geom_venn(aes(set_name = set_name, elements = elements), data = venn4_df) # Use theme_void() to get a plain background ggplot() + geom_venn(aes(set_name = set_name, elements = elements), data = venn4_df) + theme_void()
# Give circles discrete fills (discrete Venn type) ggplot() + geom_venn(aes(set_name = set_name, elements = elements, fill = fill), type = "discrete", data = venn4_df) + scale_fill_identity() + theme_void()
# Fill each overlap based on the count (continuous Venn type) ggplot() + geom_venn(aes(set_name = set_name, elements = elements, fill = count), type = "continuous", data = venn4_df) + scale_fill_gradientn(colours = alpha(c("white", "red"), 0.7)) + theme_void()
# Custom set name position new_pos <- data.frame( x = c(-1.5, -1, 1, 1.5), y = c(-1.2, 1.5, 1.5, -1.2) ) ggplot() + geom_venn(aes(set_name = set_name, elements = elements, fill = fill), type = "discrete", data = venn4_df, set_name_pos = new_pos) + scale_fill_identity()
# Add set totals ggplot() + geom_venn(aes(set_name = set_name, elements = elements, fill = fill), type = "discrete", data = venn4_df, set_total = TRUE) + scale_fill_identity() + theme_void()
# Adjust percentage y position down a little ggplot() + geom_venn(aes(set_name = set_name, elements = elements, fill = fill), type = "discrete", data = venn4_df, percentage_nudge = -0.09) + scale_fill_identity() + theme_void() # Adjust set total y position above ggplot() + geom_venn(aes(set_name = set_name, elements = elements, fill = fill), type = "discrete", data = venn4_df, set_total = TRUE, set_total_pos = c(0, 0.2)) + scale_fill_identity() + theme_void()
p <- ggplot() + geom_venn(aes(set_name = set_name, elements = elements), type = "discrete", data = venn2_df, percentage_nudge = -0.08) + annotate("curve", x = -1.2, xend = -0.1, y = -1.2, yend = -0.2, curvature = 0.3, arrow = arrow(length = unit(2, "mm"))) + annotate("text", x = -1.25, y = -1.2, label = "Interesting!", hjust = "right") + labs(title = "Venn Diagram") p p + theme_void()
count_venn()
count_venn(lst)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.