How To Make A Venn Diagram in R

Channel:
Subscribers:
53,300
Published on ● Video Link: https://www.youtube.com/watch?v=saFyipc7Wd8



Category:
Guide
Duration: 5:05
24,430 views
298


Venn diagrams are plots used to graphically display intersections between two or more groups. In this code clip we learn how to construct Venn diagrams in R using the ggplot2 and VennDiagram packages.

#datavisualization #rprogramming #rstats

Code used in this clip:

# Venn diagram with ggplot
library(tidyverse)
library(ggforce) # Adds geom_circle

# DataFrame to specify circle and text positions
data <- data.frame(x = c(0, 1, -1),
y = c(-0.5, 1, 1),
tx = c(0, 1.5, -1.5),
ty = c(-1, 1.3, 1.3),
cat = c('Domain Experience', 'Math',
'Computer Science'))

# Plot circles and text; add annotations for overlapping segments
ggplot(data, aes(x0 = x , y0 = y, r = 1.5, fill = cat)) +
geom_circle(alpha = 0.25, size = 1, color = "black",show.legend = FALSE) +
geom_text(aes(x = tx , y = ty, label = cat), size = 7) +
annotate(geom="text", x=0, y=1.5, label="Machine \n Learning",color="purple", size = 5) +
annotate(geom="text", x=-0.9, y=0, label="Traditional \n Software",color="darkorange", size = 5) +
annotate(geom="text", x=0.9, y=0, label="Traditional \n Research",color="darkgreen", size = 5) +
annotate(geom="text", x=0, y=0.5, label="Data \n Science",color="blue", size = 5) +
theme_void()

# Set plot display size
options(repr.plot.height=8, repr.plot.width=8)


# Venn diagram with VennDiagram package

# Load the VennDiagram package
library(VennDiagram)

set.seed(12)
set1 <- sample(1:10000, 2000)
set2 <- sample(1:10000, 2000)
set3 <- sample(1:10000, 2000)
set4 <- sample(1:10000, 2000)
set5 <- sample(1:10000, 2000)
colors <- c("#6b7fff", "#c3db0f", "#ff4059", "#2cff21", "#de4dff")

# Make Venn diagram from list of groups
venn.diagram(x = list(set1, set2, set3, set4, set5) ,
category.names = c("s1", "s2", "s3", "s4", "s5"),
filename = 'datadaft_venn.png',
output=TRUE,
imagetype="png",
scaled = FALSE,
col = "black",
fill = colors,
cat.col = colors,
cat.cex = 2,
margin = 0.15
)

# Display saved image
options(repr.plot.height=12, repr.plot.width= 12)
library("png")
pp <- readPNG("datadaft_venn.png")
plot.new()
rasterImage(pp,0,0,1.1,1.1)


Resourced used to help make this video:
https://scriptsandstatistics.wordpress.com/2018/04/26/how-to-plot-venn-diagrams-using-r-ggplot2-and-ggforce/

https://www.r-graph-gallery.com/14-venn-diagramm.html

http://www.di.fc.ul.pt/~jpn/r/GraphicalTools/Venn.html


Code Clips are basic code explanations intended to be short reference guides that provide quick breakdowns and copy/paste access to code needed to accomplish common data science tasks.

* Note: YouTube does not allow greater than or less than symbols in the text description, so the code above may not be exactly the same as the code shown in the video! For R that means I may use = for assignment and the special Unicode large < and > symbols in place of the standard sized ones for dplyr pipes and comparisons. These special symbols should work as expected for R code on Windows, but may need to be replaced with standard greater than and less than symbols for other operating systems.







Tags:
venn diagram r
r venn diagram
how to make a venn diagram in r
how to make a venn diagram
venn
diagram
venn diagram ggplot
venn diagram ggplot2
ggplot venn diagram
ggplot2 venn diagram
make a venn diagram in r
make a venn diagram in ggplot
data visuialization
venn diagrams in r
venn diagrams in ggplot
venn diagrams in ggplot2
r plotting tutorial
r plots
venn diagrams
make venn diagrams
make a venn diagram