This is a test version of Biostars. For the public version, visit https://www.biostars.org.
ggVennDiagram help

Hi all,

I'm creating Venn's using ggVennDiagram and I can print the intersection tibble using the process_region_data() function but I cannot figure out how to print the components of each intersection (the lists of intersection elements)? Can anyone help me here? Ultimately I want to extract these to a csv.

Cheerio,
B

r ggvenndiagram

1 answer

Hi, it is a feature that I also struggled to find a solution so I needed to customize some a code to make it work as I want.

So here is my solution. I have found a very useful code on GitHub : https://github.com/hms-dbmi/UpSetR/issues/85#issuecomment-415480954 I adapted it to make it more readable so here is my final code :

overlapGroups <- function (listInput, sort = TRUE) {
  listInputmat    <- fromList(listInput) == 1
  listInputunique <- unique(listInputmat)
  grouplist <- list()
  for (i in 1:nrow(listInputunique)) {
    currentRow <- listInputunique[i,]
    myelements <- which(apply(listInputmat,1,function(x) all(x == currentRow)))
    attr(myelements, "groups") <- currentRow
    grouplist[[paste(colnames(listInputunique)[currentRow], collapse = ":")]] <- myelements
    myelements
  }
  if (sort) {
    grouplist <- grouplist[order(sapply(grouplist, function(x) length(x)), decreasing = TRUE)]
  }
  attr(grouplist, "elements") <- unique(unlist(listInput))
  return(grouplist)
}

Intersection=overlapGroups(yourlist)
purrr::map(Intersection, ~ attr(Intersection, "elements")[.x] )

Example :

yourlist=list(A=c("a","b","c","d"),B=c("a","c","e","f"))
Intersection=overlapGroups(yourlist)
purrr::map(Intersection, ~ attr(Intersection, "elements")[.x] )
$`A:B`
[1] "a" "c"

$A
[1] "b" "d"

$B
[1] "e" "f"

Log in to answer this question.