如何使用R来绘制韦恩图(Venn Diagram)

2020-08-06 14:42:18 浏览数 (3)

2017.12.15日厦大生命科学前沿课上,一位博士师姐分享的一篇论文中,多次应用韦恩图,看起来特别美,于是特地去R语言官网阅读关于Venn Diagram的使用。从网上高手博客中看到一些例子,故敲一遍代码以熟悉Venn Diagram包的使用。

> install.packages("VennDiagram")

> library(VennDiagram)

> A = 1:150

> B = c(121:170,300:320)

> C = c(20:40,141:200)

> Length_A<-length(A)

> Length_B<-length(B)

> Length_C<-length(C)

> Length_AB<-length(intersect(A,B))

> Length_BC<-length(intersect(B,C))

> Length_AC<-length(intersect(A,C))

> Length_ABC<-length(intersect(intersect(A,B),C))

> T<-venn.diagram(list(A=A,B=B),filename=NULL,lwd=1,lty=2, ,col=c('red','green'),fill=c('red','green'),cat.col=c('red','green'),rotation.degree=90)

> grid.draw(T)

> T<-venn.diagram(list(A=A,B=B,C=C),filename=NULL,lwd=1,lty=2,col=c('red','green','blue') ,fill=c('red','green','blue'),cat.col=c('red','green','blue'),reverse=TRUE)

> grid.draw(T)

>draw.pairwise.venn(area1=Length_A,area2=Length_B,cross.area=Length_AB,category=c('A','B'),lwd=rep(1,1),lty=rep(2,2),col=c('red','green'),fill=c('red','green'),cat.col=c('red','green'),rotation.degree=90)

这种方式画的图与第一种方式画出结果一样!接下来利用draw.triple.venn绘制三个集合的韦恩图

> draw.triple.venn(area1=Length_A, area2=Length_B, area3=Length_C,n12=Length_AB, n23=Length_BC, n13=Length_AC, n123=Length_ABC,category = c('A','B','C'),col=c('red','green','blue'),fill=c('red','green','blue'),cat.col=c('red','green','blue')

,reverse = FALSE)

傻瓜式代码如下:

> install.packages("VennDiagram")

> library(VennDiagram)

> A <- c("C", "D", "B", "E") #生成三个不同的集合

> B <- c("C", "E", "B", "A")

> C <- c("C", "E", "X", "Y")

> venn.diagram(x=list(Tumor=A, Organoids=B, Primary=C), "My1.png", height = 450, width = 450, resolution =300, imagetype="png", col="white", fill=c(colors()[616], colors()[38], colors()[468]), alpha=c(0.6, 0.6, 0.6), lwd=c(1, 1, 1), cex=0, cat.dist=c(-0.07, -0.07, -0.05), cat.pos=c(300, 60, 180), cat.cex=0.45) #height、width、resolution等的含义参考help()函数即可了解,这里备注下“My 1.png“是图片保存文件名称,倘若不想以文件的形式保存,则filename必须为空,即filename=NULL。

> D <- c("C", "D", "B", "E") #生成三个不同的组合

> E <- c("C", "F", "G", "K")

> F <- c("C", "H", "X", "Y")

>venn.diagram(x=list(Tumor=D, Organoids=F,Primary=E), "My2.png", height = 450, width = 450, resolution =300, imagetype="png", col="gray",fill=c(colors()[148], colors()[589], colors()[116]), alpha=c(0.6, 0.6, 0.6), lwd=0.5, cex=0, cat.dist=c(-0.07, -0.07, -0.05), cat.pos=c(100, 260, 0),cat.cex=0.45, cat.col="black", rotation.degree=180)

> A<- sample(1:1000, 400, replace = FALSE) #生成四个不同的组合

> B<- sample(1:1000, 600, replace = FALSE)

> C<- sample(1:1000, 350, replace = FALSE)

> D<- sample(1:1000, 550, replace = FALSE)

> venn.diagram(x= list(A = A,D = D,B = B,C = C), filename = "My3.png", height = 450, width = 450,resolution =300, imagetype="png", col ="transparent", fill =c("cornflowerblue","green","yellow","darkorchid1"),alpha = 0.5, label.col = c("orange", "white","darkorchid4", "white", "white", "white", "white", "white","darkblue", "white", "white", "white","white", "darkgreen", "white"), cex = 0.45,fontfamily = "serif", fontface = "bold",cat.col =c("darkblue", "darkgreen", "orange","darkorchid4"), cat.cex = 0.45,cat.pos = 0, cat.dist = 0.07,cat.fontfamily = "serif", rotation.degree = 270)

> A<- sample(LETTERS, 18, replace = FALSE)

> B<- sample(LETTERS, 20, replace = FALSE)

> C<- sample(LETTERS, 22, replace = FALSE)

> D<- sample(LETTERS, 19, replace = FALSE)

> venn.diagram(x= list(A = A,D = D,B = B,C = C), filename = "My4.png", height = 450, width = 450,resolution =300, imagetype="png", col="transparent",fill=c("cornflowerblue","green","yellow","darkorchid1"),alpha = 0.50, cex=0.45, cat.cex=0.45)

> venn.diagram(x =list(A = A,D = D,B = B,C = C), filename = "My5.png", height = 450, width= 450, resolution =300, imagetype="png", lwd=0.6, fill =c("cornflowerblue","green","yellow","darkorchid1"),cex=0.45, cat.cex=0.45)

当然韦恩图会画,最重要的还是要会分析!

1 人点赞