在R中制作漂亮的五组Venn图

15

我最近看到一篇很好的五组文氏图,如下:

enter image description here

有没有人知道如何用 R 编写生成这样文氏图的代码?

我查看的大多数软件包似乎生成了不太清晰的版本:

enter image description here

有什么想法吗?

编辑:看起来 Nature 文章使用了这个 Web 工具:http://bioinformatics.psb.ugent.be/webtools/Venn/ 可能将其移植到某些 R 包中仍然不错,尤其是目前任何我知道的软件包中都没有的非对称文氏图。


1
你是否检查了Venn Diagrams with R中的程序包? - CL.
是的,我尝试了所有这些!但它们只能生成底部的图形,而不能生成顶部的图形!而且有些限制为4个或更少的集合... - Tom Wenseleers
4个回答

33
Adrian Dusa最近开发了一个非常好用的新R包venn,可以制作如上所示的Venn图,最多支持7个集合。
library(venn)
venn(5, ilab=TRUE, zcolor = "style")

enter image description here

venn(7, ilab=TRUE, zcolor = "style")

enter image description here

感谢Adrian送来的酷包裹!
对于许多数据集来说,使用UpSet图可能更容易阅读: https://github.com/hms-dbmi/UpSetR

enter image description here


你能提供一个代码示例吗? - Herman Toothrot
1
如果你还在寻找代码,请访问:https://github.com/dusadrian/venn - undefined

9

我还在Ghent大学生物信息学网站上使用了在线的Venn图生成器,我也想要自己的函数。当然,想法是让每个交集区域的大小合理,以便计数的文本可以是相同的大小。这里是我的尝试:

fiveCellVenn <- function(colorList=col2rgb(rainbow(5)),
                         cellCounts=seq(1,31,1),
                         cellLabels=c("one","two","three","four","five"),
                         saturation=.25){
  rotateVec <-function(vec,amount){
    return(c(vec[(amount+1):length(vec)], vec[1:amount]))
  }
  n=70
  xhull <- c()
  yhull <- c()
  for (i in 1:n){
    xhull <- c(xhull, cos((i*2*pi)/n))
    yhull <- c(yhull, sin((i*2*pi)/n))
  }
## The Venn cells begin as a 70-sided regular polygon
## plot(xhull, yhull)
## polygon(xhull, yhull)
## Multiply each unit vector in the hull by a scalar, arrived at by 
## iterative adjustment.
  adjust <-c(10,10.35,10.6,10.5,10.4,10.3,10.1,9.6,9,8.5,
             8,7.625,7.25,7.125,7,6.875,6.75,6.875,7,7.125,
             7.25,7.625,8.1,9.125,10.25,11.375,12.5,13.15,13.8,14.3,
             14.6,14.725,14.7,14.7,14.7,14.4,14.1,13.8,13.5,12.8,
             12.1,11.15,10.2,9.6,8.95,8.3,7.7,7,6.25,5.6,
             5,4.75,4.5,4.25,4,3.8,3.6,3.45,3.45,3.45,
             3.5,3.625,3.75,3.825,4,4.25,4.5,5.75,7.25,8.5)
  newxhull <- xhull*adjust
  newyhull <- yhull*adjust
## Text location was also done by hand:
  textLocationX <- c(-13,-3,8,9,-4,-7.5,7.5,-9,-8,4,6.5,-2,9,-4,1.4,4,-7.5,-3.5,7.5,-6,-6.5,6,1.5,4,-0.5,4.5,0,-5,-3.5,3.5,0)
  textLocationY <- c(1,12,8,-8,-12,6.5,4.5,1,-4,8.5,-6,8.5,-1.5,-8,-9,5,3.5,6,1.5,-6,-1.5,-2,8,-7,-6.5,2,5.5,2,-3.5,-4,0)
  textLocationMatrix <- matrix(cbind(textLocationX,textLocationY),nrow=31,ncol=2)
  plot(newxhull, newyhull, pch=".", xlim=c(-16,16), ylim=c(-16,16),
       axes=FALSE,xlab="",ylab="")
  newAdjust<-adjust
  for (i in 1:5){
    newxhull <- xhull*newAdjust
    newyhull <- yhull*newAdjust
    polygon(newxhull, newyhull, 
            border=rgb(colorList[1,i]/255, colorList[2,i]/255, colorList[3,i]/255, 1),
            lwd=2,
            col=rgb(colorList[1,i]/255, colorList[2,i]/255, colorList[3,i]/255,saturation))
    newAdjust <- rotateVec(newAdjust,14)
  }
  text(textLocationMatrix[,1], textLocationMatrix[,2],labels=as.character(cellCounts))
  text(textLocationMatrix[c(17,2,3,24,14),1]*c(2,1.25,1.5,2,2),
       textLocationMatrix[c(17,2,3,24,14),2]*c(2,1.35,1.5,2,2),
       labels=cellLabels)
##  uncomment and run to get points and grid for adjusting text location
##  points(textLocationMatrix[,1], textLocationMatrix[,2])
##  for (i in -16:16){
##   if (i%%5==0){
##     color="black"
##   }else{
##     color="lightblue"
##   }
##  abline(v=i,col=color)
##  abline(h=i, col=color)
##  }
}

那么

fiveCellVenn()

Venn图像

使用上述代码可以生成类似于第一个Venn图。我还没有足够的声望来发布图片。您可能需要减弱颜色并移动单元格名称。


1
非常好!非常感谢您 - 看起来很棒!您还可以考虑稍微平滑多边形,使它们看起来更漂亮,例如http://gis.stackexchange.com/questions/24827/how-to-smooth-the-polygons-in-a-contour-map/24929#24929。也可以考虑将其推广到其他集合编号。也许可以联系其中一个Venn图R包 - 他们可能有兴趣包含这个! - Tom Wenseleers
使用上面链接中的spline.poly()函数,可以使用类似于plot(newxhull,newyhull); polygon(spline.poly(as.data.frame(list(x=newxhull,y=newyhull))[-seq(1, length(newxhull), by = 2),],100,k=3))的方法来对多边形进行平滑处理。 - Tom Wenseleers

3
library(venn); library(tidyverse); library(stringr); 

p_th = 0.0;

data <- read_csv("finaldf.csv")
data

venn = 
  list(A = 
     data %>% 
     filter(CVA > p_th) %>% 
     .$phrase,
   B = 
     data %>% 
     filter(IHD > p_th) %>% 
     .$phrase,
   C = 
     data %>% 
     filter(CM > p_th) %>% 
     .$phrase,
   D = 
     data %>% 
     filter(ARR > p_th) %>% 
     .$phrase,
   E = 
     data %>% 
     filter(VD > p_th) %>% 
     .$phrase,
   G = 
     data %>% 
     filter(CHD > p_th) %>% 
     .$phrase);

png("ven.png", width = 800, height = 800)

venn.result =
  venn(venn, ilabels = TRUE, 
       zcolor = "style", size = 25, cexil = 1.2, cexsn = 1.5);


dev.off()

6个集合的维恩图

IMG


这看起来很不错。谢谢分享!你能否通过在你的答案中添加 dput(data) 来使其更有用和可重现性? - Tung
1
@Dibakar,请问 final_df.csv 文件长什么样? - user2110417

0
也许你可以尝试使用VennDetail,它不仅可以帮助你生成维恩图,还可以提供生成“维恩饼图”图形的选项。你还可以根据图形轻松提取每个子集。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接