如何用6个集合绘制维恩图

2024-07-03 06:42:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个包含6个元素的数据集。在

我想画一个维恩图,但是用R(Venndiagram库)它被限制在5个元素。在

你知道怎么画6组维恩图吗?也许用python?在

我的数据示例:

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
...

Tags: 数据元素示例mdmcountsathppervenndiagram
3条回答

可以构造更多组件的维恩图(>5),但由于其非常难以理解,因此它们变得非常复杂以至于不再有用(参见https://en.wikipedia.org/wiki/Venn_diagram以了解具有6个元素的Venn图的一些示例)。在

UpSet包(http://www.caleydo.org/tools/upset/),它提供了一种不同的方法来可视化更多数量的数据集,对于这样的任务可能更有用。在

谢谢你的回答和你的罚款维恩js图书馆。如果用户想用新的htmlwidget来解决R中的问题,下面是我的答案。这不是最有效的方法,但它是有效的。在

# 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 )

我不久前写了一个javascript库,可以做到这一点:venn.js。它在您给定的数据集上生成以下输出:

Output on your data

它是用javascript编写的,但是Kent Russell提供了一个htmlwidgets interface to call it from R。在

生成代码是:

// define set sizes and their intersection sizes
var sets = [
    {sets: ['Ath'], size: 901},
    {sets: ['Fve'], size: 14764},
    {sets: ['Mdm'], size: 19408},
    {sets: ['Pcmm'], size: 17714},
    {sets: ['Pper'], size: 16948},
    {sets: ['Pmum'], size: 17572},
    {sets: ['Ath', 'Fve'], size: 823},
    {sets: ['Ath', 'Mdm'], size: 846},

    // need to specfiy the rest of the sets don't overlap
    {sets: ['Ath', 'Pcmm'], size: 0},
    {sets: ['Ath', 'Pper'], size: 0},
    {sets: ['Ath', 'Pmum'], size: 0},
    {sets: ['Fve', 'Mdm'], size: 0},
    {sets: ['Fve', 'Pcmm'], size: 0},
    {sets: ['Fve', 'Pper'], size: 0},
    {sets: ['Fve', 'Pmum'], size: 0},
    {sets: ['Mdm', 'Pcmm'], size: 0},
    {sets: ['Mdm', 'Pper'], size: 0},
    {sets: ['Mdm', 'Pmum'], size: 0},
    {sets: ['Pcmm', 'Pper'], size: 0},
    {sets: ['Pcmm', 'Pmum'], size: 0},
    {sets: ['Pper', 'Pmum'], size: 0},
    ];

// draw the chart in the venn div
var chart = venn.VennDiagram();
d3.select("#venn").datum(sets).call(chart);

相关问题 更多 >