R中的组合值用于维恩图

时间:2018-05-04 06:16:47

标签: r venn-diagram

我想在圆圈内绘制圆圈。我已经成功创建了一个三元组,我的代码中包含以下细分:

venn.plot <- draw.triple.venn(
area1 = 1171,
area2 = 979,
area3 = 579,
n12 = 979,
n13 = 579,
n23 = 579,
n123 = 579,

最小的圆圈为579,下一个圆圈增加400,总共为979,下一个圆圈增加192,总共为1171.

我想将此扩展到5个级别,下一个将52添加为1223,最后添加11为1234。

我无法计算组合值以成功生成图表。我得到的错误是

ERROR [2018-05-04 16:05:48] Impossible: a5 <- area5 - a6 - a7 - a15 - a16 - a17 - a18 - a25 - a26 - a27 - a28 - a31 - a20 - a29 - a21 - a10 produces negative area Error in draw.quintuple.venn(area1 = 1234, area2 = 1223, area3 = 1171,  :   Impossible: a5 <- area5 - a6 - a7 - a15 - a16 - a17 - a18 - a25 - a26 - a27 - a28 - a31 - a20 - a29 - a21 - a10 produces negative area

显然我加错了。

我应该使用的n值是多少?     n12 =     n13 =     n14 =     n15 =     n23 =     n24 =     n25 =     n34 =     n35 =     n45 =     n123 =     n124 =     n125 =     n134 =     n135 =     n145 =     n234 =     n235 =     n245 =     n345 =     n1234 =     n1235 =     n1245 =     n1345 =     n2345 =     n12345 =

1 个答案:

答案 0 :(得分:2)

如果我理解你希望每一组都是另一组的子集。这是电话:

library(VennDiagram)
venn.plot <- draw.quintuple.venn(
  area1 = 579L,
  area2 = 979L,
  area3 = 1171L,
  area4 = 1223L,
  area5 = 1234L,
  n12 = 579L,
  n13 = 579L,
  n14 = 579L,
  n15 = 579L,
  n23 = 979L,
  n24 = 979L,
  n25 = 979L,
  n34 = 1171L,
  n35 = 1171L,
  n45 = 1223L,
  n123 = 579L,
  n124 = 579L,
  n125 = 579L,
  n134 = 579L,
  n135 = 579L,
  n145 = 579L,
  n234 = 979L,
  n235 = 979L,
  n245 = 979L,
  n345 = 1171L,
  n1234 = 579L,
  n1235 = 579L,
  n1245 = 579L,
  n1345 = 579L,
  n2345 = 979L,
  n12345 = 579L)

http://square.github.io/picasso/

每个区域交叉点等于参与其中区域的最小值。

现在这太麻烦了,特别是如果你需要经常执行它,这是一种编程方式:

定义你的集合:

val = c(579, 979, 1171, 1223, 1234)

将其命名为

names(val) ;- LETTERS[1:length(val)]

也许有更优雅的方式,但从我的头脑:

z <- lapply(1:length(val),function(x){
  comb <- combn(names(val), m = x) #all combination of 1:5 elements of names
  value <- apply(comb, 2, function(i){ #for each combination return minumum from val vector
    return(min(val[names(val) %in% i]))
  })
  set <- apply(comb, 2, paste0, collapse = "&", sep = "") #paste the names with "&" in beetwean
  names(value) = set
  return(value)
})

分别运行每行代码(例如使用3而不是x)来理解它的作用。

z <- unlist(z)

library(eulerr) # a nice library
plot(euler(z, input = "union"), quantities = T)

enter image description here

让它成为一个函数:

create_eulerr_data <- function(val){
  z <- lapply(1:length(val),function(x){
  comb <- combn(names(val), m = x)
  value <- apply(comb, 2, function(i){
    return(min(val[names(val) %in% i]))
  })
  set <- apply(comb, 2, paste0, collapse = "&", sep = "")
  names(value) = set
  return(value)
  })
  z <- unlist(z)
  }

现在关于这一点的好处是它适用于任意数量的集合:

val <- c(26, 53, 78)
names(val) <- LETTERS[1:length(val)]
plot(euler(create_eulerr_data(val),
           input = "union"),
     quantities = T)

enter image description here

val <- c(26, 53, 78, 112, 134, 158)
names(val) <- LETTERS[1:length(val)]
plot(euler(create_eulerr_data(val),
           input = "union"),
     quantities = T)

enter image description here

即使您提供未经安排的值,它也会起作用:

val <- c(26, 78, 53)
names(val) <- LETTERS[1:length(val)]
plot(euler(create_eulerr_data(val),
           input = "union"),
     quantities = T)

enter image description here