R 获取Venn图中的项目列表

R 获取Venn图中的项目列表,r,venn-diagram,R,Venn Diagram,使用以下代码绘制维恩图很容易: library(VennDiagram) set.seed(1) # For reproducibility of results xx.1 <- list(A = sample(LETTERS, 15), B = sample(LETTERS, 15), C = sample(LETTERS, 15), D = sample(LETTERS, 15)) venn.diagram(xx.1, filename ="1.tiff

使用以下代码绘制维恩图很容易:

library(VennDiagram)

set.seed(1) # For reproducibility of results
xx.1 <- list(A = sample(LETTERS, 15), B = sample(LETTERS, 15), 
             C = sample(LETTERS, 15), D = sample(LETTERS, 15))

venn.diagram(xx.1, filename ="1.tiff", height = 1000, width = 1000)
库(VennDiagram)
设定种子(1)#用于结果的再现性

xx.1查看
?intersect
?union
?setdiff
函数,以提取维恩图的不同字段

我已经创建了两个函数的一些
列表
版本,以便更好地获取不同隔间中的元素:

Intersect <- function (x) {  
  # Multiple set version of intersect
  # x is a list
  if (length(x) == 1) {
    unlist(x)
  } else if (length(x) == 2) {
    intersect(x[[1]], x[[2]])
  } else if (length(x) > 2){
    intersect(x[[1]], Intersect(x[-1]))
  }
}

Union <- function (x) {  
  # Multiple set version of union
  # x is a list
  if (length(x) == 1) {
    unlist(x)
  } else if (length(x) == 2) {
    union(x[[1]], x[[2]])
  } else if (length(x) > 2) {
    union(x[[1]], Union(x[-1]))
  }
}

Setdiff <- function (x, y) {
  # Remove the union of the y's from the common x's. 
  # x and y are lists of characters.
  xx <- Intersect(x)
  yy <- Union(y)
  setdiff(xx, yy)
}

您还可以在
gplots
包中使用
venn
来获取venn图各部分中的项目列表(“项目列表”)。根据您的清单xx.1,它应该是:

ItemsList <- venn(xx.1, show.plot = FALSE)
在VennDiagram包中,它有一个名为“calculate.overlap”的函数


这将是伟大的加入工会的结果!(只是为了得到完整的例子)我对一个更复杂的过程做了或多或少的相同操作,但是你的解决方案是简单而出色的@艾比格劳,谢谢。是一种系统地获取所有15个字段的方法吗?@AEBilgrau,真是太棒了@Llopis我还添加了一个递归的
联合。很好的建议,谢谢。@al ash的答案似乎更易于使用。获取交叉点的名称,即a1的含义,似乎不可行。
set.seed(1)
xx.1 <- list(A = sample(LETTERS, 15), 
             B = sample(LETTERS, 15), 
             C = sample(LETTERS, 15), 
             D = sample(LETTERS, 15))
Intersect(xx.1)
#[1] "E" "L"
Setdiff(xx.1[c("C", "D")], xx.1[c("A", "B")])
#[1] "O" "P" "K" "H"
# Create a list of all the combinations
combs <- 
  unlist(lapply(1:length(xx.1), 
                function(j) combn(names(xx.1), j, simplify = FALSE)),
         recursive = FALSE)
names(combs) <- sapply(combs, function(i) paste0(i, collapse = ""))
str(combs)
#List of 15
# $ A   : chr "A"
# $ B   : chr "B"
# $ C   : chr "C"
# $ D   : chr "D"
# $ AB  : chr [1:2] "A" "B"
# $ AC  : chr [1:2] "A" "C"
# $ AD  : chr [1:2] "A" "D"
# $ BC  : chr [1:2] "B" "C"
# $ BD  : chr [1:2] "B" "D"
# $ CD  : chr [1:2] "C" "D"
# $ ABC : chr [1:3] "A" "B" "C"
# $ ABD : chr [1:3] "A" "B" "D"
# $ ACD : chr [1:3] "A" "C" "D"
# $ BCD : chr [1:3] "B" "C" "D"
# $ ABCD: chr [1:4] "A" "B" "C" "D"

# "A" means "everything in A minus all others"
# "A", "B" means "everything in "A" and "B" minus all others" and so on
elements <- 
  lapply(combs, function(i) Setdiff(xx.1[i], xx.1[setdiff(names(xx.1), i)]))

n.elements <- sapply(elements, length)
print(n.elements)
#   A    B    C    D   AB   AC   AD   BC   BD   CD  ABC  ABD  ACD  BCD ABCD 
#   2    2    0    0    1    2    2    0    3    4    4    1    1    2    2 
ItemsList <- venn(xx.1, show.plot = FALSE)
lengths(attributes(ItemsList)$intersections)
# A       B     A:B     A:C     A:D     B:D     C:D   A:B:C   A:B:D   A:C:D   B:C:D A:B:C:D 
# 2       2       1       2       2       3       4       4       1       1       2       2
overlap <- calculate.overlap(xx.1)
$a6
[1] "C"

$a12
[1] "Z" "D" "R"

$a11
[1] "Y" "O" "V"

$a5
[1] "X" "B"

$a7
[1] "H" "F" "P" "S"

$a15
[1] "I"

$a4
[1] "L" "K" "G"

$a10
[1] "W" "J"

$a13
[1] "U"

$a8
character(0)

$a2
character(0)

$a9
character(0)

$a14
[1] "N" "M"

$a1
[1] "E"

$a3
[1] "Q" "A" "T"