how to draw Venn Diagram using 6 sets

前端 未结 3 1587
闹比i
闹比i 2021-01-07 04:45

I have a dataset with 6 elements.

An I would like to draw a venn diagram, but with R (Venndiagram library) it\'s limited at 5 elements.

Do you know how can

3条回答
  •  臣服心动
    2021-01-07 05:42

    Thanks @ben-frederickson for both the answer and your fine venn.js library. If the user would like to solve the problem in R with the new d3vennR htmlwidget, here is my answer. It is not the most efficient method, but it works.

    # devtools::install_github("timelyportfolio/d3vennR")
    
    library(d3vennR)
    library(sets)
    
    sets_df <- read.csv(
      textConnection("Ath,Fve,Mdm,Pcmm,Pper,Pmum,Counts
    1,0,0,0,0,0,901
    0,1,0,0,0,0,14764
    0,0,1,0,0,0,19408
    0,0,0,1,0,0,17714
    0,0,0,0,1,0,16849
    0,0,0,0,0,1,17572
    1,1,0,0,0,0,823
    1,0,1,0,0,0,846"
      )
    )
    
    # get all sets provided and their counts/size
    sets_list <- apply(
      sets_df
      ,MARGIN=1
      ,function(rw){
        list(
          sets = as.list(colnames(sets_df)[which(rw==1)])
          , size = as.numeric(tail(rw,1))
        )
      }
    )
    
    # get all set combinations to fill with size = 0 where missing
    sets_combinations <- lapply(
      # answer by venn.js authors only goes to combinations of m=2
      #  this goes to combinations of m = sets - 1
      seq.int(1,length(colnames(sets_df))-2)
      ,function(m){
        t(combn(colnames(sets_df)[-length(colnames(sets_df))],m=m))
      }
    )
    
    # now combine the sets and sizes provided in data with the sets and 0 
    sets_venn <- unlist(
      lapply(
        sets_combinations
        ,function(x){
          apply(
            x
            ,MARGIN=1
            ,function(y){
              # this gets sets of 0 which are not in the data provided
              if(!set_contains_element(
                as.set(lapply(sets_list,function(z){as.set(unlist(z$sets))}))
                ,as.set(y)
              )){
                list(sets=y,size=0)
              } else {
              # this gets sets with their sizes given by the partial data provided
                unlist(
                  Filter(
                    function(z){
                      set_is_equal(as.set(y),as.set(unlist(z$sets)))
                    }
                    ,sets_list
                  )
                  ,recursive=F
                )
              }
            }
          )
        }
      )
      ,recursive=F
    )
    
    
    # produce the Venn Diagram in R with the transformed data
    d3vennR( data = sets_venn )
    

提交回复
热议问题