Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Venn Diagrams with `venneuler` in `R`: delete the name of the set from the plot and add elements name

Tags:

r

venn-diagram

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.

like image 534
Mark Avatar asked Oct 30 '25 22:10

Mark


1 Answers

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)

like image 68
Waldi Avatar answered Nov 02 '25 12:11

Waldi



Donate For Us

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