【问题标题】:How to draw Venn diagram using R如何使用 R 绘制维恩图
【发布时间】:2013-10-19 14:21:11
【问题描述】:

我有三个 ID 列表。

我想比较这 3 个列表,并绘制一个维恩图。在获得的维恩图中,我将在交叉点显示的不是数字而是 ID。 我需要在 R 中做到这一点,但我真的不知道怎么做。 你可以帮帮我吗? 那是我的代码。它有效,但只显示数字,我会在交叉点中显示“术语”

       set1 <- unique(goterm1)
       set2 <- unique(goterm2)
        set3 <- unique(goterm3)

       require(limma)
       Diagram <- function(set1, set2, set3, names)
       {
     stopifnot( length(names) == 3)
      # Form universe as union of all three sets
      universe <- sort( unique( c(set1, set2, set3) ) )
      Counts <- matrix(0, nrow=length(universe), ncol=3)
      colnames(Counts) <- names
        for (i in 1:length(universe))
        {
        Counts[i,1] <- universe[i] %in% set1
        Counts[i,2] <- universe[i] %in% set2
       Counts[i,3] <- universe[i] %in% set3
       }

         vennDiagram( vennCounts(Counts) )}

       Diagram(set1, set2, set3, c("ORG1", "ORG2", "ORG3"))
        Venn

【问题讨论】:

  • venneuler 包应该可以帮到你;如果不是,VennDiagram 包更可定制。不过,您很可能必须事先弄清楚您感兴趣的比例。
  • Thanks, Ive 尝试了两种方法都没有结果。但是我还没有看到任何好的例子来学习它。
  • 现在你需要发帖dput(goterm1) ; dput(goterm2);dput(goterm2)

标签: r venn-diagram


【解决方案1】:

你也可以和 limma 一起完成这项壮举。请参阅下面的示例。

这个想法与您发布的代码基本完全相同,但它没有被包装到函数中(因此可能更容易调试)。

你是否让它与下面的代码一起工作?如果没有,请发布您收到的可能的错误消息和警告。

# Load the library
library(limma)

# Generate example data
set1<-letters[1:5]
set2<-letters[4:8]
set3<-letters[5:9]

# What are the possible letters in the universe?
universe <- sort(unique(c(set1, set2, set3)))

# Generate a matrix, with the sets in columns and possible letters on rows
Counts <- matrix(0, nrow=length(universe), ncol=3)
# Populate the said matrix
for (i in 1:length(universe)) {
   Counts[i,1] <- universe[i] %in% set1
   Counts[i,2] <- universe[i] %in% set2
   Counts[i,3] <- universe[i] %in% set3
}

# Name the columns with the sample names
colnames(Counts) <- c("set1","set2","set3")

# Specify the colors for the sets
cols<-c("Red", "Green", "Blue")
vennDiagram(vennCounts(Counts), circle.col=cols)

代码应该给出类似于:

【讨论】:

  • 来自 CRAN 的包 limma 似乎不适用于 R 3.1.1,但我使用 source("http://bioconductor.org/biocLite.R") biocLite("limma") 让它工作了
【解决方案2】:

这可以使用我的 r 包eulerr 来完成,如下所示:

set1 <- letters[1:5]
set2 <- letters[4:8]
set3 <- letters[5:9]

library(eulerr)

plot(euler(list(A = set1, B = set2, C = set3)))

【讨论】:

  • 这似乎是一个很棒,非常易于使用的功能,但是用户无法设置圆圈的颜色:(
  • 更新:可以设置颜色:plot(euler(list(A = set1, B = set2, C = set3)), fills = c("red", "blue", "green" ))
【解决方案3】:

此答案使用dev version of qdaptrans_venn 函数。这是venneuler package 的包装器,在我的工作流程中很有意义,但我认为可以在这里应用。

安装dev version of qdap:

library(devtools)
install_github("qdapDictionaries", "trinker")
install_github("qdap", "trinker")

将其应用于您的数据:

set1 <- letters[1:5]
set2 <- letters[4:8]
set3 <- letters[5:9]

## reshapes the list of vectors to a data frame (unique is not needed)
dat <- list2df(list(set1 = set1, set2 = set2, set3 = set3), "word", "set")
trans_venn(dat$word, dat$set)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多