|
# 安装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)
回复关键词“Venn”,获取脚本源码和测试数据下载路径。
1、小编对R语言自带的文件读入函数进行参数优化,让读取数据更高效,小伙伴们可以用200M以上大小进行测试。 2、对于新手,小编建议先使用他人写好的函数将结果运行出来,然后进行尝试拆分函数,提高自己的编程能力。 3、在本程序中,小编使用两个函数保存图片,一种是ggplot2包中的ggsave函数,这个函数存储的图片比R自带的图片函数存储的图片质量更高,由于venn包与ggplot2语法不兼容,所以用venn绘制的图片用R语言自带的函数存储。 各位小伙伴,以上的教程学会了吗? 如果需要下载本脚本以及本案例中的数据文件,请关注本公众号(密码子实验室:mimazilab)并回复关键词“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()
Archiver|手机版|科学网 ( 京ICP备07017567号-12 )
GMT+8, 2024-11-26 10:16
Powered by ScienceNet.cn
Copyright © 2007- 中国科学报社