【问题标题】:How to make venn diagrams in R?如何在R中制作维恩图?
【发布时间】:2012-07-30 13:22:15
【问题描述】:

嗨,我是 R 新手,我必须用它来制作维恩图。我已经用谷歌搜索了一段时间,我能找到的所有示例都处理二进制变量。但是我有 2 个列表(实际上是 2 个 csv 文件)。列表中的项目只是字符串,例如 PSF113_xxxx。我必须对它们进行比较,看看它们各自的独特之处和共享的东西。我如何在 R 中用这个制作维恩图?

此外,文件中的东西数量也不相同,一个比另一个稍微多一点,这意味着 cbind 函数返回错误。

到目前为止,我已经想出了这个,但这只是给我一个名为 group 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

左边的编号不是我做过的,它补充说,当我从 excel 将文件导入 R 时

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

【问题讨论】:

  • 提供可重现的数据示例将使您走得更远。

标签: r venn-diagram


【解决方案1】:

您的代码仍然不能完全重现,因为您还没有定义 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)

这种方法的不同之处在于我取消列出了两个数据帧(如果它们是数据帧),然后将它们转换为字符向量。我认为这应该可行。

【讨论】:

  • A 和 B 只是我导入的 csv 文件,实际上它们是 A = open.csv(...)。如果我打电话给 A 或 B,我给出的数据示例。不过,谢谢。我会试试这个
  • 试试head(A, 10)head(B)
  • 我从您的代码中收到一条错误消息,指出排序函数的数据必须是原子的。你能帮忙吗?就像我之前所说的,当我调用我的数据时,它是以我在 OP 中显示的形式给出的
  • @TheFox 检查此链接以获取可重复的示例:stackoverflow.com/questions/5963269/… 对于我们不理解的内容,我们无能为力。我让你使用head(A, 10)head(B, 10),但你没有。当您寻求帮助但没有提供适当的信息时,就像去看医生并说很疼一样。伤害什么也没告诉他。我无法重现您的错误,因为我从未超过您代码中的第一行。
  • 我在帖子中包含了 Head(a) 和 b。对不起,我以前没有,但我以前没有,因为它和我之前给的完全一样
猜你喜欢
  • 2022-11-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多