I found that the package VennDiagram has a function calculate.overlap() but I wasn't able to find a way to name the sections from this function. However, if you use package gplots , there is the function venn() which will return the intersections attribute.
AW.DL <- c("a","b","c","d")
AW.FL <- c("a","b", "e", "f")
AW.UL <- c("a","c", "e", "g")
library(gplots)
lst <- list(AW.DL,AW.FL,AW.UL)
ItemsList <- venn(lst, show.plot = FALSE)
lengths(attributes(ItemsList)$intersections)
Output:
> lengths(attributes(ItemsList)$intersections)
A B C A:B A:C B:C A:B:C
1 1 1 1 1 1 1
To get elements, just print attributes(ItemsList)$intersections:
> attributes(ItemsList)$intersections
$A
[1] "d"
$B
[1] "f"
$C
[1] "g"
$`A:B`
[1] "b"
$`A:C`
[1] "c"
$`B:C`
[1] "e"
$`A:B:C`
[1] "a"
A wrapper based on @itsDV7
getVennOverlap <- function(lsvenn = list(A = sort(sample(LETTERS, 15)),
B = sort(sample(LETTERS, 15)),
C = sort(sample(LETTERS, 15)),
D = sort(sample(LETTERS, 15)))
) {
ItemsList <- gplots::venn(lsvenn, show.plot = FALSE)
print(lengths(attributes(ItemsList)$intersections))
return(attributes(ItemsList)$intersections)
}
Test
> getVennOverlap()
A B D A:B A:C A:D B:C B:D C:D A:B:C A:B:D A:C:D
1 1 1 1 2 1 3 1 1 1 3 3
B:C:D A:B:C:D
2 3
$A
[1] "T"
$B
[1] "N"
$D
[1] "L"
$`A:B`
[1] "I"
...etc
Take a look at ?intersect, ?union and ?setdiff function to extract the different fields of the Venn diagram.
I have created some list versions of the two functions to better get the elements in the different compartments:
Intersect <- function (x) {
# Multiple set version of intersect
# x is a list
if (length(x) == 1) {
unlist(x)
} else if (length(x) == 2) {
intersect(x[[1]], x[[2]])
} else if (length(x) > 2){
intersect(x[[1]], Intersect(x[-1]))
}
}
Union <- function (x) {
# Multiple set version of union
# x is a list
if (length(x) == 1) {
unlist(x)
} else if (length(x) == 2) {
union(x[[1]], x[[2]])
} else if (length(x) > 2) {
union(x[[1]], Union(x[-1]))
}
}
Setdiff <- function (x, y) {
# Remove the union of the y's from the common x's.
# x and y are lists of characters.
xx <- Intersect(x)
yy <- Union(y)
setdiff(xx, yy)
}
So, if we want to see the common elements (i.e. the union of A, B, C, and D) or the ones in C and D but not in A and B in your example we do something like the following.
set.seed(1)
xx.1 <- list(A = sample(LETTERS, 15),
B = sample(LETTERS, 15),
C = sample(LETTERS, 15),
D = sample(LETTERS, 15))
Intersect(xx.1)
#[1] "E" "L"
Setdiff(xx.1[c("C", "D")], xx.1[c("A", "B")])
#[1] "O" "P" "K" "H"
Hope this helps!
Edit: Systematically get all components
By some (I think) clever use of the combn function, indexing, and a good understanding of lapply we can all elements systematically:
# Create a list of all the combinations
combs <-
unlist(lapply(1:length(xx.1),
function(j) combn(names(xx.1), j, simplify = FALSE)),
recursive = FALSE)
names(combs) <- sapply(combs, function(i) paste0(i, collapse = ""))
str(combs)
#List of 15
# $ A : chr "A"
# $ B : chr "B"
# $ C : chr "C"
# $ D : chr "D"
# $ AB : chr [1:2] "A" "B"
# $ AC : chr [1:2] "A" "C"
# $ AD : chr [1:2] "A" "D"
# $ BC : chr [1:2] "B" "C"
# $ BD : chr [1:2] "B" "D"
# $ CD : chr [1:2] "C" "D"
# $ ABC : chr [1:3] "A" "B" "C"
# $ ABD : chr [1:3] "A" "B" "D"
# $ ACD : chr [1:3] "A" "C" "D"
# $ BCD : chr [1:3] "B" "C" "D"
# $ ABCD: chr [1:4] "A" "B" "C" "D"
# "A" means "everything in A minus all others"
# "A", "B" means "everything in "A" and "B" minus all others" and so on
elements <-
lapply(combs, function(i) Setdiff(xx.1[i], xx.1[setdiff(names(xx.1), i)]))
n.elements <- sapply(elements, length)
print(n.elements)
# A B C D AB AC AD BC BD CD ABC ABD ACD BCD ABCD
# 2 2 0 0 1 2 2 0 3 4 4 1 1 2 2
You can also use venn in gplots package to get a list of items in each section of venn diagram ('ItemsList'). Given your list xx.1, it should be:
ItemsList <- venn(xx.1, show.plot = FALSE)
ItemsList contains:
- a matrix of all diagram sections and the counts of items in these sections and
- the list of items in each Venn diagram section.
to get the counts:
lengths(attributes(ItemsList)$intersections)
# A B A:B A:C A:D B:D C:D A:B:C A:B:D A:C:D B:C:D A:B:C:D
# 2 2 1 2 2 3 4 4 1 1 2 2
Another simple example that shows how to print a Venn diagram using the VennDiagram package:
library(VennDiagram)
cardiome <- letters[1:10]
superset <- letters[8:24]
overlap <- calculate.overlap(
x <- list("Cardiome"=cardiome, "SuperSet"=superset))
venn.plot <- draw.pairwise.venn(
area1 = length(cardiome),
area2 = length(superset),
cross.area = length(overlap),
category = c("Cardiome", "Superset"),
fill = c("blue", "red"),
lty = "blank",
cex = 2,
cat.cex = 2,
cat.pos = c(180, 180),
cat.dist = 0.05,
cat.just = list(c(0, 1), c(1, 1))
)
grid.draw(venn.plot)
savePlot(filename="venndiag", type="png")

Venn diagrams with item labels inside the sets:
library(RAM)
vectors <- list(Cardiome=cardiome, Superset=superset)
group.venn(vectors=vectors, label=TRUE,
fill = c("blue", "red"),
cat.pos = c(180, 180),
lab.cex=1.1)

The funtion venn.diagram() does it. For instance in your example
venn.diagram(x = list(
"Cardiome" = cardiome,
"SuperSet" = superset
), "plot_venn")
It saves to working directory. Type getwd() to see what it is set to.
See the
?venn.diagram()
for more info.
By replacing x in overlap[[x]] with red number 1-15, you can get a complete list of genes of interest at the specific location in Venn diagram.
Also, you can get the numbers of genes by using length() function.

Sorry, I need to point out that it is wrong and could be misleading. I've oulined the correct answer below:
a6 = n1234;
a12 = n123[-which(n123 %in% a6)];
a11 = n124[-which(n124 %in% a6)];
a5 = n134[-which(n134 %in% a6)];
a7 = n234[-which(n234 %in% a6)];
a15 = n12[-which(n12 %in% c(a6,a11,a12))];
a4 = n13[-which(n13 %in% c(a6,a5,a12))];
a10 = n14[-which(n14 %in% c(a6,a5,a11))];
a13 = n23[-which(n23 %in% c(a6,a7,a12))];
a8 = n24[-which(n24 %in% c(a6,a7,a11))];
a2 = n34[-which(n34 %in% c(a6,a5,a7))];
a9 = A[-which(A %in% c(a4,a5,a6,a10,a11,a12,a15))];
a14 = B[-which(B %in% c(a6,a7,a8,a11,a12,a13,a15))];
a1 = C[-which(C %in% c(a2,a4,a5,a6,a7,a12,a13))];
a3 = D[-which(D %in% c(a2,a5,a6,a7,a8,a10,a11))];