Plotting a Venn diagram given its structure

61 Views Asked by At

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?

1

There are 1 best solutions below

0
Stéphane Laurent On

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)