如何在R中创建维恩图?
问题描述:
嗨,我是R新手,我必须用它来制作维恩图。我一直在使用Google搜索一段时间,所有的例子,我可以找到处理二进制变量。不过,我有2个列表(其实2个CSV文件)。列表中的项目只是字符串,如PSF113_xxxx。我必须将它们进行比较,以了解每种方式的独特性以及共享的内容。我如何在R中制作一个维恩图?如何在R中创建维恩图?
此外文件中没有相同数量的东西,其中一个略多于另一个,这意味着cbind函数返回错误。
到目前为止,我已经提出了这个问题,但这只是给我一个名为组1的图像,里面有1个,外面是0。
matLis <- list(matrix(A), matrix(B))
n <- max(sapply(matLis, nrow))
do.call(cbind, lapply(matLis, function (x)
rbind(x, matrix(, n-nrow(x), ncol(x)))))
x = vennCounts(n)
vennDiagram(x)
这是我做过数据
2 PSF113_0018
3 PSF113_0079
4 PSF113_0079a
5 PSF113_0079b
左侧的编号是没有什么的一个例子,它补充说,当我导入文件分成R从Excel
head(A)
> head(A)
V1
1 PSF113_0016a
2 PSF113_0018
3 PSF113_0079
4 PSF113_0079a
5 PSF113_0079b
6 PSF113_0079c
> head(b,10)
V1
1 PSF113_0016a
2 PSF113_0021
3 PSF113_0048
4 PSF113_0079
5 PSF113_0079a
6 PSF113_0079b
7 PSF113_0079c
8 PSF113_0295
9 PSF113_0324a
10 PSF113_0324b
答
由于您没有定义A或B,因此您的代码仍然不太可重复。下面是包venneuler
中的维恩图指南,因为我发现它更灵活。
List1 <- c("apple", "apple", "orange", "kiwi", "cherry", "peach")
List2 <- c("apple", "orange", "cherry", "tomatoe", "pear", "plum", "plum")
Lists <- list(List1, List2) #put the word vectors into a list to supply lapply
items <- sort(unique(unlist(Lists))) #put in alphabetical order
MAT <- matrix(rep(0, length(items)*length(Lists)), ncol=2) #make a matrix of 0's
colnames(MAT) <- paste0("List", 1:2)
rownames(MAT) <- items
lapply(seq_along(Lists), function(i) { #fill the matrix
MAT[items %in% Lists[[i]], i] <<- table(Lists[[i]])
})
MAT #look at the results
library(venneuler)
v <- venneuler(MAT)
plot(v)
编辑:头是因为它为我们提供了一些工作非常有帮助。试试这个办法:
#For reproducibility (skip this and read in the csv files)
A <- structure(list(V1 = structure(1:6, .Label = c("PSF113_0016a",
"PSF113_0018", "PSF113_0079", "PSF113_0079a", "PSF113_0079b",
"PSF113_0079c"), class = "factor")), .Names = "V1",
class = "data.frame", row.names = c("1",
"2", "3", "4", "5", "6"))
B <- structure(list(V1 = structure(1:10, .Label = c("PSF113_0016a",
"PSF113_0021", "PSF113_0048", "PSF113_0079", "PSF113_0079a",
"PSF113_0079b", "PSF113_0079c", "PSF113_0295", "PSF113_0324a",
"PSF113_0324b"), class = "factor")), .Names = "V1",
class = "data.frame", row.names = c("1",
"2", "3", "4", "5", "6", "7", "8", "9", "10"))
运行代码从这里:
#after reading in the csv files start here
Lists <- list(A, B) #put the word vectors into a list to supply lapply
Lists <- lapply(Lists, function(x) as.character(unlist(x)))
items <- sort(unique(unlist(Lists))) #put in alphabetical order
MAT <- matrix(rep(0, length(items)*length(Lists)), ncol=2) #make a matrix of 0's
colnames(MAT) <- paste0("List", 1:2)
rownames(MAT) <- items
lapply(seq_along(Lists), function(i) { #fill the matrix
MAT[items %in% Lists[[i]], i] <<- table(Lists[[i]])
})
MAT #look at the results
library(venneuler)
v <- venneuler(MAT)
plot(v)
这种方法的区别是,我不公开的两个数据帧(如果他们dataframes),然后把它们以字符向量。我认为这应该工作。
提供一个可重复使用的数据示例会让您更进一步。 – 2012-07-30 13:34:56