muzi0202的个人博客分享 http://blog.sciencenet.cn/u/muzi0202

博文

高颜值多维venn图——你学会了吗?

已有 4982 次阅读 2020-9-12 10:17 |系统分类:科研笔记

维恩图(Venn diagram),也称韦恩图、文氏图、温氏图,是利用集合分类的原理将特定的数据进行分类或归类。
在生信分析中,Venn图可用于统计多组或多个样本中共有和独有的物种的(OUT、Gene)数目,可以比较直观的展现不同环境样本中物种的OTU或者样本之间的Gene组成相似性及重叠情况,一般用于2~7组数据的比较分析。绘出的图一般长这样:


结果图.png


分析方法
# 安装R包if (!requireNamespace("VennDiagram", quietly = TRUE))  install.packages("VennDiagram",repos = "https://mirrors.tuna.tsinghua.edu.cn/CRAN/")if (!requireNamespace("ggplot2", quietly = TRUE))  install.packages("ggplot2",repos = "https://mirrors.tuna.tsinghua.edu.cn/CRAN/")if (!requireNamespace("venn", quietly = TRUE))  install.packages("venn",repos = "https://mirrors.tuna.tsinghua.edu.cn/CRAN/")if (!requireNamespace("RColorBrewer", quietly = TRUE))  install.packages("RColorBrewer",repos = "https://mirrors.tuna.tsinghua.edu.cn/CRAN/")if (!requireNamespace("data.table", quietly = TRUE))  install.packages("data.table",repos = "https://mirrors.tuna.tsinghua.edu.cn/CRAN/")
# 自定义函数## 快速读入数据readFlie=function(input,type,row=T,header=T){  # input 为读入文件的路径,type为读入文件的类型,格式为‘.txt’或‘.csv’,row=T,将文件的第一列设置为列名  library(data.table,quietly = TRUE)  if(type=='txt'){    dat = fread(input,header = header,sep='\t',stringsAsFactors = F,check.names = F)    if(row){      dat = as.data.frame(dat,stringsAsFactors = F)      rownames(dat) = dat[,1]      dat = dat[,-1]    }else{      dat = as.data.frame(dat,stringsAsFactors = F)    }  }else{    dat = fread(input,header = header,sep=',',stringsAsFactors = F,check.names = F)    if(row){      dat = as.data.frame(dat,stringsAsFactors = F)      rownames(dat) = dat[,1]      dat = dat[,-1]    }else{      dat = as.data.frame(dat,stringsAsFactors = F)    }  }  return(dat)}## 绘制venn图wn_venn=function(list,col='black'){  # 定义颜色体系  library(RColorBrewer,quietly = TRUE)  corlor = brewer.pal(8,'Dark2')  # 绘制Venn图  library(VennDiagram, quietly=TRUE)  library(venn,quietly = TRUE)  if(length(list)<=7){    if(length(list)<=4){      graphics=venn.diagram(list,filename=NULL,fill = corlor[1:length(list)],                            col = col,alpha = 0.5, cat.cex = 1.5,rotation.degree = 0)      grid.draw(graphics)    }else if(length(list)==5){      graphics=venn(list, zcolor = corlor[1:length(list)],box=F,ellipse =TRUE,cexil = 1, cexsn = 1)    }else{      graphics=venn(list, zcolor = corlor[1:length(list)],box=F,cexil = 1, cexsn = 1)    }    return(graphics)  }else{    print('The function only supports data of dimension 7 and below.')  }}## 保存图片,只支持ggplot对象savePlots=function(path,plot,type=c('pdf','png','tiff')[1],width=10,height=8,dpi=300){  # path表示保存图片路径,需要加上相应的文件扩展名称  library(ggplot2)  if(type=='pdf'){    ggsave(filename = path,plot = plot,width = width,height = height,device = 'pdf')  }else if(type=='png'){    ggsave(filename = path,plot = plot,width = width,height = height,device = 'png',dpi = dpi)  }else{    ggsave(filename = path,plot = plot,width = width,height = height,device = 'tiff',dpi = dpi)  }}

实战演练


关注微信公众号(密码子实验室,mimazilab

公众号关注二维码.png

回复关键词“Venn”,获取脚本源码和测试数据下载路径。

# 读入数据df = readFlie('./venn.txt',type = 'txt',row = F)# 抽取数据,制造测试数据set.seed(1234)df_list = list('Symbol1'=sample(df$symbol,'Symbol2'=200),'Symbol3'=220),sample(df$symbol,               sample(df$symbol,'Symbol6'=280),'Symbol7'=<span 0px;max-width:="" 1000%;box-sizing:="" border-box="" !important;word-wrap:="" break-word="" !important;color:="" #ca7d37\"="" style="overflow-wrap: break-word; float: none;">sample(df$symbol,300))# 绘制venn图## 4维veen图fg_4 = wn_venn(df_list[1:4])## 5维veen图fg_5 = wn_venn(df_list[1:5])## 7维veen图fg_7 = wn_venn(df_list)# 保存图片savePlots(path = './fg_4.pdf',plot = fg_4,type = 'pdf',width = 10,height = 10)savePlots(path = './fg_4.png',plot = fg_4,type = 'png',width = 10,height = 10,dpi = 300)savePlots(path = './fg_4.tiff',plot = fg_4,type = 'tiff',width = 10,height = 10,dpi = 600)
pdf('./fg5.pdf',width = 10,height = 10)fg_5 = wn_venn(df_list[1:5])dev.off()pdf('./fg7.pdf',width = 10,height = 10)fg_7 = wn_venn(df_list)dev.off()
程序说明

1、小编对R语言自带的文件读入函数进行参数优化,让读取数据更高效,小伙伴们可以用200M以上大小进行测试。

2、对于新手,小编建议先使用他人写好的函数将结果运行出来,然后进行尝试拆分函数,提高自己的编程能力。

3、在本程序中,小编使用两个函数保存图片,一种是ggplot2包中的ggsave函数,这个函数存储的图片比R自带的图片函数存储的图片质量更高,由于venn包与ggplot2语法不兼容,所以用venn绘制的图片用R语言自带的函数存储。

       

       各位小伙伴,以上的教程学会了吗?

       如果需要下载本脚本以及本案例中的数据文件,请关注本公众号(密码子实验室:mimazilab并回复关键词“Venn”,即可获得下载链接。


image.png




https://blog.sciencenet.cn/blog-3445347-1250244.html

上一篇:一个从FASTA文件中提取特定序列的脚本
下一篇:AI科研绘图—细胞膜速成大法
收藏 IP: 223.68.32.*| 热度|

2 王汉森 张鹰

该博文允许注册用户评论 请点击登录 评论 (0 个评论)

数据加载中...

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-4-27 13:23

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部