Create Venn Diagram Using ggvenn Package in R (Example Code)

In this R tutorial you’ll learn how to create venn diagrams using the ggvenn package.

Creation of Example Data

set.seed(65354)                     # Example data for venn diagram
df <- data.frame(value = 1:10,
                 Set1 = sample(c(TRUE, FALSE), 10, replace = TRUE),
                 Set2 = sample(c(TRUE, FALSE), 10, replace = TRUE),
                 Set3 = sample(c(TRUE, FALSE), 10, replace = TRUE),
                 Set4 = sample(c(TRUE, FALSE), 10, replace = TRUE))
df
#    value  Set1  Set2  Set3  Set4
# 1      1 FALSE  TRUE  TRUE FALSE
# 2      2 FALSE  TRUE  TRUE FALSE
# 3      3  TRUE  TRUE  TRUE  TRUE
# 4      4  TRUE FALSE FALSE FALSE
# 5      5  TRUE  TRUE FALSE  TRUE
# 6      6  TRUE  TRUE FALSE  TRUE
# 7      7  TRUE FALSE  TRUE FALSE
# 8      8  TRUE  TRUE FALSE  TRUE
# 9      9 FALSE  TRUE FALSE  TRUE
# 10    10 FALSE  TRUE FALSE FALSE

Example: Draw Venn Diagram with geom_venn Function of ggvenn Package

library("ggvenn")
library("ggplot2")
ggplot(df,                          # Using geom_venn() function
       aes(A = Set1,
           B = Set2,
           C = Set3,
           D = Set4)) +
  geom_venn()

r graph figure 1 create venn diagram using ggvenn package r

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu
Top