For the past few days I have been trying to figure out how to draw a Venn diagram from an arbitrary number of sets and came across the R package venneuler. This code:
genes <- paste("gene",1:20,sep="")
df = data.frame(term=character(), class=character(), stringsAsFactors = FALSE)
for (k in 1:15) {
  df2=data.frame(term=sample(genes,10), class = rep(paste("class ",k,sep=""),10), stringsAsFactors = 
  FALSE)
  df<-rbind(df,df2)
}
library(rJava)
library(UpSetR)
library(tidyverse)
library(venneuler)
library(grid)
v <- venneuler(df)
par(cex = 0.5) 
plot(v)
produces a figure like this:

This figure was just what I was looking for. Anyway, I would like to remove the name of the set (class 1, class 2 etc.) from the plot, and instead add the elements (e.g. gene1, gene2) contained within each set.
You could directly modify v$labels:
library(venneuler)
library(dplyr)
library(stringr)
v <- venneuler(df)
par(cex = 0.5) 
# Create the list of genes per class
classgenes <- df %>% group_by(class) %>% 
                     summarize(labels = paste(stringr::str_sort(term,numeric=T),collapse = '\n'))  %>%
                     ungroup 
# Order the class new label according to v$labels order
newlabels <- left_join(data.frame(class = v$labels), classgenes)
#> Joining, by = "class"
# Modify the labels
v$labels <- newlabels$labels
plot(v)

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With