Tutorial: Using ggVennDiagram

ggVennDiagram’ enables fancy Venn plot with 2-7 sets and generates publication quality figure.

Installation

You can install the released version of ggVennDiagram from CRAN with:

install.packages("ggVennDiagram")

And the development version from GitHub with:

# install.packages("devtools")
devtools::install_github("gaospecial/ggVennDiagram")

Usage

Generate example data.

genes <- paste0("gene",1:1000)
set.seed(20210302)
gene_list <- list(A = sample(genes,100),
                  B = sample(genes,200),
                  C = sample(genes,300),
                  D = sample(genes,200))

library(ggVennDiagram)
library(ggplot2)

long category names

If you use long category names in Venn plot, labels may be cropped by plot borders. To avoid this, just use a ggplot trick to expand x axis.

p1 <- ggVennDiagram(gene_list, 
                    category.names = c("a very long name","short name","name","another name"))
p1


# expand axis to show long set labels
p1 + scale_x_continuous(expand = expansion(mult = .2))

Show intersection values

When intersection values only have several members, ggVennDiagram is efficient to show the values in places.

set.seed(0)
small_list <- lapply(sample(6:10, size = 4), function(x){
  sample(letters,x)
})

ggVennDiagram(small_list, 
              category.names = LETTERS[1:4], 
              show_intersect = TRUE)
#> Warning in geom_text(aes(label = .data$count, text = .data$item), data =
#> region_label): Ignoring unknown aesthetics: text

Setting set label

color of set label

ggVennDiagram(gene_list, set_color = c("blue","black","red","yellow"))

size of set labels

ggVennDiagram(gene_list, set_size = 8)

Setting region label

text content

ggVennDiagram(gene_list, label = "count")
ggVennDiagram(gene_list, label = "percent")
ggVennDiagram(gene_list, label = "both")
ggVennDiagram(gene_list, label = "none")

percentage digits

ggVennDiagram(gene_list, label_percent_digit = 1, label = "percent")

remove label background

  • Method 1: set alpha to 0
ggVennDiagram(gene_list, label_alpha = 0)

  • Method 2: use geom_text()
ggVennDiagram(gene_list, label_geom = "text")

color and size

ggVennDiagram(gene_list, label_color = "firebrick", label_size = 4)

Setting set edges

ggVennDiagram(gene_list, edge_lty = "dashed", edge_size = 1)

Changing palette

  • changing fill palette
library(ggplot2)
p <- ggVennDiagram(gene_list)

# Red Blue
p + scale_fill_distiller(palette = "RdBu")


# Reds
p + scale_fill_distiller(palette = "Reds", direction = 1)

Some other palletes are:

RColorBrewer::display.brewer.all()

Adding note

p + labs(title = "Fancy Venn Diagram of four sets",
         subtitle = "Generated by `ggVennDiagram`",
         caption = Sys.Date())