Plotting All Subgroups - Venn Diagram

I've been doing R recently and I have an interesting problem. I have to plot all possible venn diagrams from 9 single column datasets (call them df1 ... df9). I am using library (gplots) and code snippet for venn

with example input:

venn(list(df1,df2,df3,df4))

      

The question arises: how can I generate all possible subsets of these nine datasets (126 in total - a computed function of the ridges used.) And export them into a list that can be injected into venn. So, for example: (DF1, df2), (DF5, DF4), (DF3, DF5, DF8),,, Here I will go through all the options and plot venn for each. Thanks for any hints.

+3


source to share


1 answer


Let's start with a reproducible example:

# Sample data (9 15-element subsets of the letters stored in a list)
set.seed(144)
(dfs <- replicate(9, sample(letters, 15), simplify=FALSE))
# [[1]]
#  [1] "b" "r" "y" "l" "g" "n" "a" "u" "z" "s" "j" "c" "h" "x" "m"
# 
# [[2]]
#  [1] "b" "n" "m" "t" "i" "f" "a" "l" "k" "u" "o" "c" "g" "v" "p"
# ...

      

The function venn

does not support venn diagrams with more than 5 sets, and venn diagrams with one set are rather uninteresting. Therefore, I'll limit the subsets to two to five sets:

# Get all subsets with 2-5 elements
subs <- do.call(expand.grid, replicate(length(dfs), c(F, T), simplify=F))
subs <- subs[rowSums(subs) %in% 2:5,]
venns <- apply(subs, 1, function(x) venn(dfs[x], show.plot=F))

      

venns

now contains all 372 of your venn diagram objects. You can build a specific object likeplot(venns[[100]])



enter image description here

If you really want to plot all venn diagrams, you can do something like:

apply(subs, 1, function(x) {
  png(paste0("venn_", paste(which(x), collapse="_"), ".png"))
  venn(dfs[x])
  dev.off()
})

      

This will create 372 image files containing venn diagrams named according to the sets that are included.

+8


source







All Articles