Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotting a Venn diagram given its structure

Tags:

r

venn-diagram

In R, I have a Venn diagram encoded as follows:

[[1]]
[[1]][[1]]
[1] FALSE  TRUE # B

[[1]][[2]]
[1] 1


[[2]]
[[2]][[1]]
[1] TRUE TRUE # A and B

[[2]][[2]]
[1] 1


[[3]]
[[3]][[1]]
[1]  TRUE FALSE # A

[[3]][[2]]
[1] 2

That means there are two sets, say A and B, because the first element of each list is a vector of two Boolean values. The Venn diagram has three zones, because there are three lists. The second element of each list gives then number of elements included in the corresponding zone.

How can I plot this Venn diagram?

like image 445
Stéphane Laurent Avatar asked May 02 '26 01:05

Stéphane Laurent


1 Answers

I think I've found, using ggVennDiagram::plot_venn:

library(ggVennDiagram)
nsets <- 2
shd <- get_shape_data(nsets)
id <- shd$regionLabel$id

newDiagram <- integer(length(diagram))
ids <- nms <- character(length(diagram))
for(i in seq_along(diagram)) {
  ids[i] <- paste0((1:nsets)[diagram[[i]][[1]]], collapse = "/")
  nms[i] <- paste0(LETTERS[1:nsets][diagram[[i]][[1]]], collapse = "/")
  newDiagram[i] <- diagram[[i]][[2]]
}
names(newDiagram) <- ids

library(tibble)
shd$regionData <- tibble(id = ids, count = newDiagram[ids], name = nms)
shd$regionLabel$count <- newDiagram[id]
shd$regionLabel$name <- nms
shd$setData <- tibble(name = LETTERS[1:nsets])
shd$setLabel$name <- LETTERS[1:nsets]

plot_venn(shd)
like image 124
Stéphane Laurent Avatar answered May 03 '26 14:05

Stéphane Laurent