Jose Gracia Rodriguez
Jose Gracia Rodriguez

Reputation: 167

How to obtain the list of elements from a Venn diagram

I have a Venn diagram made from 3 lists, I would like to obtain all the different sub-lists, common elements between two lists, between the tree of them, and the unique elements for each list. Is there a way to make this as straight forward as possible?

AW.DL <- c("a","b","c","d")

AW.FL <- c("a","b", "e", "f")

AW.UL <- c("a","c", "e", "g")

venn.diagram(
  x = list(AW.DL, AW.FL, AW.UL),
  category.names = c("AW.DL" , "AW.FL","AW.UL" ),
  filename = '#14_venn_diagramm.png',
  output=TRUE,
  na = "remove"
)

Upvotes: 0

Views: 3673

Answers (2)

bud.dugong
bud.dugong

Reputation: 759

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

Upvotes: 0

itsDV7
itsDV7

Reputation: 854

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"

Upvotes: 2

Related Questions