R画图基本(五)文氏图vennDiagram
当前位置:以往代写 > 其他教程 >R画图基本(五)文氏图vennDiagram
2019-06-14

R画图基本(五)文氏图vennDiagram

R画图基本(五)文氏图vennDiagram

文氏图是一种非经常用的图示手段,主要用于显示组与组之间重叠的水平。

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绘制文氏图

LIMMA绘制文氏图

从上面的剧本我们可以知道,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)

gplot绘制文氏图

gplot绘制文氏图

> ##
> ## 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)

gplot绘制文氏图

gplot绘制文氏图

> ##
> ## 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)

gplot绘制文氏图

gplot绘制文氏图

可是,上面两种要领绘制出来的图都不能是彩色的。这不得不说是一个很大的缺憾。厥后又呈现两个文氏图的绘制东西包,别离是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)

#p#分页标题#e#

venneuler绘制文氏图

venneuler绘制文氏图

venneuler绘制文氏图

venneuler绘制文氏图

利用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绘制文氏图

    关键字:

在线提交作业