How to create Venn diagrams in R

Today, we will see how to create Venn diagrams in R using the VennDiagram package and the USArrests buit-in dataset.

Prepare the dataset

data("USArrests")
dataset = USArrests
dataset[, "State"] = row.names(dataset)

Compute the 75th percentile for each crime

murder = quantile(dataset[, "Murder"], 0.75)
assault = quantile(dataset[, "Assault"], 0.75)
rape = quantile(dataset[, "Rape"], 0.75)

Create Venn diagrams in R

library(VennDiagram)

venn.diagram(
  x = list(dataset[dataset[, "Murder"] >= murder, "State"],
           dataset[dataset[, "Assault"] >= assault, "State"],
           dataset[dataset[, "Rape"] >= rape, "State"]),
  
  main = "States with a high rate of murder, assault or rape",
  main.fontface = "bold",
  main.cex = 1.5,
  category.names = c("Murder", "Assault", "Rape"),
  cat.col = palette()[2:4],
  cat.cex = 1.5,
  cex = 1.5,
  scaled = FALSE,
  lty = 'blank',
  fill = palette()[2:4],
  force.unique = TRUE,
  disable.logging = TRUE,
  imagetype = "png",
  filename = "Venn_diagram.png"
)
Venn diagram created in R using VennDiagram package

Conclusion

In conclusion, the VennDiagram package is easy to use and highly customizable. Have you already used this package?

Related posts

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply