用 R 绘制维恩图?

发布于 2024-08-04 15:40:00 字数 1539 浏览 8 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

待"谢繁草 2024-08-11 15:40:00

R-forge 上有 Vennerable 包

source("http://bioconductor.org/biocLite.R")
biocLite(c("graph", "RBGL", "gtools", "xtable"))
install.packages("Vennerable", repos="http://R-Forge.R-project.org")

维恩图

There is Vennerable package on R-forge.

source("http://bioconductor.org/biocLite.R")
biocLite(c("graph", "RBGL", "gtools", "xtable"))
install.packages("Vennerable", repos="http://R-Forge.R-project.org")

Venn diagram

树深时见影 2024-08-11 15:40:00

如果您需要创建 4/5 集的维恩图,gplots 包中的维恩函数也很有用。

The venn function in the gplots package is also useful if you need to create Venn Diagram of 4/5 sets.

总攻大人 2024-08-11 15:40:00

我使用两个自定义函数来解决这个问题。我的 venndia 实现绘制了维恩图并返回 A 和 B(和 C)之间的重叠列表。请参阅下面的代码。

有了这些,您

vd <- venndia(A=LETTERS[1:15], B=LETTERS[5:20], getdata=TRUE)

就可以绘制并返回数据。你可以通过这样做关闭返回数据,

venndia(A=LETTERS[1:15], B=LETTERS[5:20])

因为默认情况下 getdata 为 FALSE。
/丹尼尔

circle <- function(x, y, r, ...) {
    ang <- seq(0, 2*pi, length = 100)
    xx <- x + r * cos(ang)
    yy <- y + r * sin(ang)
    polygon(xx, yy, ...)
}

venndia <- function(A, B, C, getdata=FALSE, ...){
    cMissing <- missing(C)
    if(cMissing){ C <- c() }

    unionAB <- union(A, B)
    unionAC <- union(A, C)
    unionBC <- union(B, C)
    uniqueA <- setdiff(A, unionBC)
    uniqueB <- setdiff(B, unionAC)
    uniqueC <- setdiff(C, unionAB)
    intersAB <- setdiff(intersect(A, B), C)
    intersAC <- setdiff(intersect(A, C), B)
    intersBC <- setdiff(intersect(B, C), A)
    intersABC <- intersect(intersect(A, B), intersect(B, C))

    nA <- length(uniqueA)   
    nB <- length(uniqueB)
    nC <- length(uniqueC)

    nAB <- length(intersAB)
    nAC <- length(intersAC)
    nBC <- length(intersBC)

    nABC <- length(intersABC)   

    par(mar=c(2, 2, 0, 0))
    plot(-10, -10, ylim=c(0, 9), xlim=c(0, 9), axes=FALSE, ...)
    circle(x=3, y=6, r=3, col=rgb(1,0,0,.5), border=NA)
    circle(x=6, y=6, r=3, col=rgb(0,.5,.1,.5), border=NA)
    circle(x=4.5, y=3, r=3, col=rgb(0,0,1,.5), border=NA)

    text( x=c(1.2, 7.7, 4.5), y=c(7.8, 7.8, 0.8), c("A", "B", "C"), cex=3, col="gray90" )

    text(
        x=c(2, 7, 4.5, 4.5, 3, 6, 4.5), 
        y=c(7, 7, 2  , 7  , 4, 4, 5), 
        c(nA, nB, nC, nAB, nAC, nBC, nABC), 
        cex=2
    )

    if(getdata){
        list(A=uniqueA, B=uniqueB, C=uniqueC, 
            AB=intersAB , AC=intersAC , BC=intersBC , 
            ABC=intersABC
        )
    }
}

I use two custom functions that to the trick. My implementation of venndia plots the venn diagram and returns lists of overlaps between A and B (and C). See the code below.

With these, you can

vd <- venndia(A=LETTERS[1:15], B=LETTERS[5:20], getdata=TRUE)

which will both plot and return the data. you can switch off returning the data by doing

venndia(A=LETTERS[1:15], B=LETTERS[5:20])

since getdata is FALSE by default.
/Daniel

circle <- function(x, y, r, ...) {
    ang <- seq(0, 2*pi, length = 100)
    xx <- x + r * cos(ang)
    yy <- y + r * sin(ang)
    polygon(xx, yy, ...)
}

venndia <- function(A, B, C, getdata=FALSE, ...){
    cMissing <- missing(C)
    if(cMissing){ C <- c() }

    unionAB <- union(A, B)
    unionAC <- union(A, C)
    unionBC <- union(B, C)
    uniqueA <- setdiff(A, unionBC)
    uniqueB <- setdiff(B, unionAC)
    uniqueC <- setdiff(C, unionAB)
    intersAB <- setdiff(intersect(A, B), C)
    intersAC <- setdiff(intersect(A, C), B)
    intersBC <- setdiff(intersect(B, C), A)
    intersABC <- intersect(intersect(A, B), intersect(B, C))

    nA <- length(uniqueA)   
    nB <- length(uniqueB)
    nC <- length(uniqueC)

    nAB <- length(intersAB)
    nAC <- length(intersAC)
    nBC <- length(intersBC)

    nABC <- length(intersABC)   

    par(mar=c(2, 2, 0, 0))
    plot(-10, -10, ylim=c(0, 9), xlim=c(0, 9), axes=FALSE, ...)
    circle(x=3, y=6, r=3, col=rgb(1,0,0,.5), border=NA)
    circle(x=6, y=6, r=3, col=rgb(0,.5,.1,.5), border=NA)
    circle(x=4.5, y=3, r=3, col=rgb(0,0,1,.5), border=NA)

    text( x=c(1.2, 7.7, 4.5), y=c(7.8, 7.8, 0.8), c("A", "B", "C"), cex=3, col="gray90" )

    text(
        x=c(2, 7, 4.5, 4.5, 3, 6, 4.5), 
        y=c(7, 7, 2  , 7  , 4, 4, 5), 
        c(nA, nB, nC, nAB, nAC, nBC, nABC), 
        cex=2
    )

    if(getdata){
        list(A=uniqueA, B=uniqueB, C=uniqueC, 
            AB=intersAB , AC=intersAC , BC=intersBC , 
            ABC=intersABC
        )
    }
}
家住魔仙堡 2024-08-11 15:40:00

这来得很晚,但对于其他寻找答案的人来说可能有用:
维恩图,CRAN 此处

它允许多组(维恩图 4 组,欧拉图 3 组)、可定制的颜色和字体、简单的语法,并且最重要的是圆圈的大小与数据集的大小成正比(至少在比较 2 个数据时)套)。安装:

install.packages("VennDiagram")
library(VennDiagram)

对于那些使用bioconductor包并使用基因组坐标的人,最近vennDiagram是在包 ChIPpeakAnno 中实现 (版本 2.5.12)并允许基因组坐标(例如 Chip-seq 峰)的漂亮交叉。对于早期采用者,您可能需要安装开发包

peaks1 = RangedData(IRanges(start = c(967654, 2010897, 2496704),
    end = c(967754, 2010997, 2496804), names = c("Site1", "Site2", "Site3")),
    space = c("1", "2", "3"), strand=as.integer(1),feature=c("a","b","f"))

peaks2 = RangedData(IRanges(start = c(967659, 2010898,2496700,3075866,3123260),
    end = c(967869, 2011108, 2496920, 3076166, 3123470),
    names = c("t1", "t2", "t3", "t4", "t5")),
    space = c("1", "2", "3", "1", "2"), strand = c(1, 1, -1,-1,1), feature=c("a","b","c","d","a"))

makeVennDiagram(RangedDataList(peaks1,peaks2, peaks1, peaks2), NameOfPeaks=c("TF1", "TF2","TF3", "TF4"),
     totalTest=100,useFeature=TRUE, main="Venn Diagram",
    col = "transparent",fill = c("cornflowerblue", "green", "yellow", "darkorchid1"),
    alpha = 0.50,label.col = c("orange", "white", "darkorchid4", "white", "white", "white", "white", "white", "darkblue", "white", "white", "white", "white", "darkgreen", "white"), cat.col = c("darkblue", "darkgreen", "orange", "darkorchid4"))

This comes very late but it might useful for others searching for an answer:
VennDiagram, on CRAN here.

It allows multiple sets (four sets for venn, 3 sets for Euler diagrams), customizable colours and fonts, simple syntax and and best of all the size of the circles is proportional to the size of the data sets (at least when comparing 2 data sets). To install:

install.packages("VennDiagram")
library(VennDiagram)

For those using bioconductor packages and working with genomic coordinates, recently vennDiagram was implemented in the package ChIPpeakAnno (version 2.5.12) and allows pretty intersections of Genomic coordinates of, for instance, Chip-seq peaks. For early adopters you might need to install the development package.

peaks1 = RangedData(IRanges(start = c(967654, 2010897, 2496704),
    end = c(967754, 2010997, 2496804), names = c("Site1", "Site2", "Site3")),
    space = c("1", "2", "3"), strand=as.integer(1),feature=c("a","b","f"))

peaks2 = RangedData(IRanges(start = c(967659, 2010898,2496700,3075866,3123260),
    end = c(967869, 2011108, 2496920, 3076166, 3123470),
    names = c("t1", "t2", "t3", "t4", "t5")),
    space = c("1", "2", "3", "1", "2"), strand = c(1, 1, -1,-1,1), feature=c("a","b","c","d","a"))

makeVennDiagram(RangedDataList(peaks1,peaks2, peaks1, peaks2), NameOfPeaks=c("TF1", "TF2","TF3", "TF4"),
     totalTest=100,useFeature=TRUE, main="Venn Diagram",
    col = "transparent",fill = c("cornflowerblue", "green", "yellow", "darkorchid1"),
    alpha = 0.50,label.col = c("orange", "white", "darkorchid4", "white", "white", "white", "white", "white", "darkblue", "white", "white", "white", "white", "darkgreen", "white"), cat.col = c("darkblue", "darkgreen", "orange", "darkorchid4"))
ぺ禁宫浮华殁 2024-08-11 15:40:00

以下是 3 变量数据的另一个版本的参考:
http://elliotnoma.wordpress.com/2011/02/09/venn-图/

代码也可以在colorfulVennPlot包中找到:
http://cran.r-project.org/web/packages/colorfulVennPlot /index.html

Here is reference to another version for 3-variable data:
http://elliotnoma.wordpress.com/2011/02/09/venn-diagram/

The code is also available in the package colorfulVennPlot:
http://cran.r-project.org/web/packages/colorfulVennPlot/index.html

夏日浅笑〃 2024-08-11 15:40:00

我推荐 VennDiagram 包:
http://cran.r-project.org/web/packages/VennDiagram /VennDiagram.pdf

在 pake 19 上,您会发现 10 个非常好的示例包(包括高级示例和简化示例)。到目前为止,我还没有发现任何它不能做但我需要它做的事情。

I would recommend the package VennDiagram:
http://cran.r-project.org/web/packages/VennDiagram/VennDiagram.pdf

On pake 19 you will find 10 pakes of very good examples (both advanced and simplified ones). As of yet I have not found anything that it can't do that I need it to do.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文