文氏图是一种非常常用的图示手段,主要用于显示组与组之间重叠的程度。
R当中可以画文氏图的包有好几个,使用起来各有特点。最原始的工具,来自于2004年的《Venn Diagrams in R》–Duncan J.Murdoch, Journal of Statistical Software. 但是,现在已经无法找到venn包了。
之后使用得较为广泛的有两个工具,一个是LIMMA内嵌的vennDiagram, 另一个是gplots当中的venn。前者最多可以对3组数据画文氏图,后者可以最多对5组数据画文氏图。首先来介绍使用LIMMA的vennDiagram.
> source("http://bioconductor.org/biocLite.R") > biocLite("limma") > library(limma) > hsb2<-read.table("http://www.ats.ucla.edu/stat/R/notes/hsb2.csv", sep=',', header=T) > attach(hsb2) > hw<-(write>=60) > hm<-(math >=60) > hr<-(read >=60) > c3<-cbind(hw, hm, hr) > a <- vennCounts(c3); a hw hm hr Counts [1,] 0 0 0 113 [2,] 0 0 1 18 [3,] 0 1 0 8 [4,] 0 1 1 8 [5,] 1 0 0 12 [6,] 1 0 1 8 [7,] 1 1 0 11 [8,] 1 1 1 22 attr(,"class") [1] "VennCounts" > vennDiagram(a, include = "both", names = c("High Writing", "High Math", "High Reading"), cex = 1, counts.col = "red") |

从上面的脚本我们可以知道,LIMMA在绘制文氏图的时候,先是对数据转换成bool类型的矩阵,而后进行分组计数,分组就包括所有可能的组合,得出数字后绘图。
在使用LIMMA绘图的时候,我们会觉得步骤太多,gplots提供了一步的解决方案,你提供list或者data.frame数据,就可以了。
> library(gplots) > ## > ## Example using a list of item names belonging to the > ## specified group. > ## > > ## construct some fake gene names.. > oneName <- function() paste(sample(LETTERS,5,replace=TRUE),collapse="") > geneNames <- replicate(1000, oneName()) > > ## > GroupA <- sample(geneNames, 400, replace=FALSE) > GroupB <- sample(geneNames, 750, replace=FALSE) > GroupC <- sample(geneNames, 250, replace=FALSE) > GroupD <- sample(geneNames, 300, replace=FALSE) > input <-list(GroupA,GroupB,GroupC,GroupD) > venn(input) |

> ## > ## Example using a list of item indexes belonging to the > ## specified group. > ## > GroupA.i <- which(geneNames %in% GroupA) > GroupB.i <- which(geneNames %in% GroupB) > GroupC.i <- which(geneNames %in% GroupC) > GroupD.i <- which(geneNames %in% GroupD) > input.i <-list(A=GroupA.i,B=GroupB.i,C=GroupC.i,D=GroupD.i) > venn(input.i) |

> ## > ## Example using a data frame of indicator ('f'lag) columns > ## > GroupA.f <- geneNames %in% GroupA > GroupB.f <- geneNames %in% GroupB > GroupC.f <- geneNames %in% GroupC > GroupD.f <- geneNames %in% GroupD > input.df <- data.frame(A=GroupA.f,B=GroupB.f,C=GroupC.f,D=GroupD.f) > venn(input.df) |

但是,上面两种方法绘制出来的图都不能是彩色的。这不得不说是一个很大的缺憾。后来又出现两个文氏图的绘制工具包,分别是venneuler以及VennDiagram。介绍它们的文献分别是:《Exact and Approximate Area-proportional Circular Venn and Euler Diagrams》– Leland Wilkinson, 以及《VennDiagram: a package for the generation of highly-customizable Venn and Euler diagrams in R》– Hanbo Chen, Paul C Boutros。在后者的文献中,有对现有文氏图绘制软件的比较表格。这里我主要总结三点:
limma::vennDiagram | gplots::venn | venneuler | VennDiagram | |
彩色 | 否 | 否 | 是 | 是 |
输入 | R object | Lists | combinations | Lists |
最大组数 | 3 | 5 | 3 | 4 |
> library("venneuler") > m <- as.matrix(data.frame(A=c(1.5, 0.2, 0.4, 0, 0), + B=c(0 , 0.2, 0 , 1, 0), + C=c(0 , 0 , 0.3, 0, 1))) > # without weights > v <- venneuler(m > 0) > plot(v) > # with weights > v <- venneuler(m) > plot(v) |
使用VennDiagram的话,用户自己可以设置的参数更多,但是却显得不易掌握。如果能再提供一些风格就好了。
> library(VennDiagram) > names(input)<-c("A","B","C","D") > venn.diagram(input,"VennDiagram.venn.png",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"),) |
你好,如何在VennDiagram中提取出重复的部分。如,最后一张图中的33具体代表哪33个重叠
这应该是在绘图之前可以获取。你可以自己写代码获取任何intersect。请在R中获取帮助:intersect
请问有支持6组或以上的包吗?
请参考:http://blog.qiubio.com:8080/archives/4191
同问,有工具或者R package可以画6个以上集合的venn图吗?
请参考如何获取venn diagram中的基因列表中的UpSetR