使用以下代码可以很容易地绘制维恩图:
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)
但是我如何计算每个字段中的项目?例如,我想知道在A中只有的两个字母是什么?
编辑:
这是我的解决方案,它不是完美的,但可以给予所有的交集。
library(reshape)
library(R.utils)
## data
A <- data.frame(names = sample(LETTERS, 15), A = 1)
B <- data.frame(names = sample(LETTERS, 15), B = 1)
C <- data.frame(names = sample(LETTERS, 15), C = 1)
D <- data.frame(names = sample(LETTERS, 15), D = 1)
## a merged data frame.
xx.1 <- list(A = A, B= B, C= C, D = D)
xx.2 <- merge_recurse(xx.1)
## function
ff.vennFourItems <- function(X)
{
## get the items from venn diagram; for four sets, there are 15 fields;
vennItems <- list()
cate.n <- names(X)[2:5]
for (i in 1:15)
{
xx.b <- intToBin(i)
## make it four bits;
if (nchar(xx.b) != 4)
{
xx.b <- paste(paste(rep("0", 4 - nchar(xx.b)), collapse = ""), xx.b, sep ="")
}
xx.b.1 <- unlist(strsplit(xx.b, ""))
xx.1 <- X
if(xx.b.1[1] == "0") { xx.1 <- xx.1[is.na(xx.1[, 2]), ] }
else { xx.1 <- xx.1[!is.na(xx.1[, 2]), ] }
if(xx.b.1[2] == "0") { xx.1 <- xx.1[is.na(xx.1[, 3]), ] }
else { xx.1 <- xx.1[!is.na(xx.1[, 3]), ] }
if(xx.b.1[3] == "0") { xx.1 <- xx.1[is.na(xx.1[, 4]), ] }
else { xx.1 <- xx.1[!is.na(xx.1[, 4]), ] }
if(xx.b.1[4] == "0") { xx.1 <- xx.1[is.na(xx.1[, 5]), ] }
else { xx.1 <- xx.1[!is.na(xx.1[, 5]), ] }
chipC <- paste(paste(cate.n, collapse = "#"), xx.b, sep = "***")
if (dim(xx.1)[1] == 0)
{
xx.2 <- list(genes = dim(xx.1)[1], chipC = chipC, chipCN = i, detailChipS = xx.1, shortL = data.frame(genes = "noInteraction", cl = i, fullCl = chipC))
}
else
{
xx.2 <- list(genes = dim(xx.1)[1], chipC = chipC, chipCN = i, detailChipS = xx.1, shortL = data.frame(genes = as.character(xx.1[, 1]), cl = i, fullCl = chipC))
}
vennItems <- c(vennItems, list(xx.2))
}
vennItems
}
xx.3 <- ff.vennFourItems(xx.2)
str(xx.3)
List of 15
$ :List of 5
..$ genes : int 1
..$ chipC : chr "A#B#C#D***0001"
..$ chipCN : int 1
..$ detailChipS:'data.frame': 1 obs. of 5 variables:
.. ..$ names: Factor w/ 25 levels "A","B","E","F",..: 25
.. ..$ A : num NA
.. ..$ B : num NA
.. ..$ C : num NA
.. ..$ D : num 1
..$ shortL :'data.frame': 1 obs. of 3 variables:
.. ..$ genes : Factor w/ 1 level "Z": 1
.. ..$ cl : int 1
.. ..$ fullCl: Factor w/ 1 level "A#B#C#D***0001": 1
$ :List of 5
..$ genes : int 0
..$ chipC : chr "A#B#C#D***0010"
..$ chipCN : int 2
4条答案
按热度按时间0vvn1miw1#
看一下
?intersect
、?union
和?setdiff
函数,以提取维恩图的不同字段。我创建了这两个函数的一些
list
版本,以更好地获取不同隔间中的元素:所以,如果我们想看到共同的元素(即A、B、C和D的并集)或C和D中的并集,但不在A和B中的并集(在您的示例中,我们将执行以下操作。
希望这有帮助!
编辑:系统获取所有组件
通过一些(我认为)巧妙地使用
combn
函数,索引,以及对lapply
的良好理解,我们可以系统地使用所有元素:zwghvu4y2#
您也可以使用
gplots
包中的venn
来获取venn图('ItemsList')每个部分中的项目列表。根据你的清单xx.1,它应该是:1.所有图表部分的矩阵以及这些部分中的项目计数,以及
1.每个维恩图部分中的项目列表。
获取计数:
xzv2uavs3#
在 VennDiagram 包中,它有一个名为“calculate.overlap”的函数。
而 overlap 就是你想要的:
mzaanser4#
How to obtain the list of elements from a Venn diagram中基于@itsDV7的 Package 器
测试