I've been dealing with R recently and I've come across an interesting problem. I have to plot all possible venn diagrams of 9 one-column datasets (let's name them df1...df9). I'm using library(gplots), and the piece of code for venn with an example input is:
venn(list(df1,df2,df3,df4))
The question is: how can I generate all possible subsets of those nine datasets (there are total of 126 - computed used combs function.), and export them in a list, which could be the inputted into venn. So for example: (df1,df2), (df5,df4), (df3,df5,df8) . . . Here I would iterate through all options and plot the venn diagram for each. Thank you for any hints.
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 venn function doesn't support venn diagrams with more than 5 sets, and venn diagrams with one set are pretty uninteresting. Therefore, I'll limit to subsets with 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 could plot a particular one with, for instance, plot(venns[[100]])

If you really wanted to plot all the venn diagrams, you might do something like:
apply(subs, 1, function(x) {
png(paste0("venn_", paste(which(x), collapse="_"), ".png"))
venn(dfs[x])
dev.off()
})
This would create 372 image files containing the venn diagrams, named based on the sets that are included.
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