4

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:

enter image description here

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.

Mark
  • 1,577
  • 16
  • 43
  • This is perhaps relevant : https://stackoverflow.com/questions/41165493/how-to-add-legends-and-values-in-a-venn-diagram-using-r-venneuler-package – Waldi Feb 03 '21 at 15:52

1 Answers1

3

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)

Waldi
  • 39,242
  • 6
  • 30
  • 78