5

I have been searching to figure out how a Venn diagram can be plotted with displaying internal labels (overlap items) programmatically. There is no error in the code, but still I cannot figure out how to fix this.

Venn diagram

  require(VennDiagram)

  AA <- c("hi","foo", "bar","yep","woo","hoo")
  BB <- c("baa","yep", "woo","yes")
  CC <- c("yes","foo","hi","woo", "huh")

  x <- list(AA=AA , BB=BB , CC=CC)


  v0 <- venn.diagram( x, filename=NULL)

  grid.draw(v0)

  overlaps <- calculate.overlap(x)
  #overlaps <- rev(overlaps)


  for (i in 1:length(overlaps)){
    v0[[i+6]]$label <- paste(overlaps[[i]], collapse = "\n") # labels start at position 7 in the list for Venn's with 3 circles
  }


  grid.newpage()
  grid.draw(v0)

enter image description here

Ibo
  • 4,081
  • 6
  • 45
  • 65

2 Answers2

6
require(VennDiagram)

AA <- c("hi","foo", "bar","yep","woo","hoo")
BB <- c("baa","yep", "woo","yes")
CC <- c("yes","foo","hi","woo", "huh")

x <- list(AA=AA , BB=BB , CC=CC)


v0 <- venn.diagram( x, filename=NULL, 
                    fill = c("red", "blue", "green"),
                    alpha = 0.50,
                    col = "transparent")

grid.draw(v0)

overlaps <- calculate.overlap(x)

# extract indexes of overlaps from list names
indx <- as.numeric(substr(names(overlaps),2,2))


# labels start at position 7 in the list for Venn's with 3 circles
for (i in 1:length(overlaps)){
  v0[[6 + indx[i] ]]$label <- paste(overlaps[[i]], collapse = "\n") 
}


grid.newpage()
grid.draw(v0)

enter image description here

Katia
  • 3,784
  • 1
  • 14
  • 27
1

I modify the code for quad venn diagram, and also fixed some some errors with the real locations of labels, this is the code:

createVennDiagramsMarkers <- function (){
    require(VennDiagram)

    x <- list()

    x$AA <- c("hi","foo", "bar","yep","woo","hoo")
    x$BB <- c("baa","yep", "woo","yes")
    x$CC <- c("yes","foo","hi","woo", "huh")
    x$DD <- c("HI","foo","bar","woo", "lg")

    v0 <<-venn.diagram(x, height=9000, width=9000,
                      col = c("red", "blue", "green", "yellow"),
                      fill = c("red", "blue", "green", "yellow"), 
                      alpha = 0.5, filename = NULL)

    overlaps <- calculate.overlap(x)
    overlaps <- rev(overlaps)


    posOverlap = as.numeric (gsub ("a","", (names (overlaps))))
    for (i in 1:length(overlaps)){
          pos = posOverlap \[i\]
          v0\[\[pos+8\]\]$label <- paste(overlaps\[\[i\]\], collapse = "\n")
    }

    pdf("venn.pdf")
    grid.draw(v0)
    dev.off()
}
createVennDiagramsMarkers ()

Quad venn diagram