Search code examples
rvenn-diagram

create venn diagram using existing counts in R?


I was wondering if I can generate a Venn diagram using vennDiagram in R without generating the counts matrix using vennCounts but having a similar matrix saved. So let's say I have this matrix:

G S P Counts

1 1 1 117898    
1 1 0 125901    
1 0 1 119360    
0 1 1 118086    
1 0 0 3505      
0 1 0 753       
0 0 1 701       
0 0 0 0 

and I call it M. when I do

m <- as.matrix(M)
vennDiagram(m)

I see:

Error in vennDiagram(m) : Can't plot Venn diagram for more than 3 sets

Is there any way around this problem? I am trying to bypass generating the matrix of 1 and 0s as I already have the counts.


Solution

  • Here's a hacky way to do it. I generate artificial sets that contain fake members composed of integers. But it works

    require(VennDiagram)
    lines = "1 1 1 117898    
    1 1 0 125901    
    1 0 1 119360    
    0 1 1 118086    
    1 0 0 3505      
    0 1 0 753       
    0 0 1 701       
    0 0 0 0"
    con <- textConnection(lines)
    data <- read.table(con)
    names(data) = c('G','S','P')
    
    close(con)
    
    sets = vector(mode = 'list', length = ncol(data)-1)
    names(sets) = names(data)[1:(ncol(data)-1)]
    lastElement = 0
    for (i in 1:nrow(data)){
        elements = lastElement:(lastElement+data[i,ncol(data)]-1)
        lastElement = elements[length(elements)]+1
        for (j in 1:(ncol(data)-1)){
            if (data[i,j]==1){
                sets[[j]]=c(sets[[j]],elements)
            }
        }
    }
    laVenn = venn.diagram(sets,filename=NULL)
    plot.new()
    grid.draw(laVenn)
    

    enter image description here