TCGA生信-可视化生信

Venn_diagram

2021-09-09  本文已影响0人  MLD_TRNA

研究背景

维恩图(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)
  }
}
 
# 读入数据
df = readFlie('./venn.txt',type = 'txt',row = F)
# 抽取数据,制造测试数据
set.seed(1234)
df_list = list('Symbol1'=sample(df$symbol,180),'Symbol2'=sample(df$symbol,200),
               'Symbol3'=sample(df$symbol,220),'Symbol4'=sample(df$symbol,240),
               'Symbol5'=sample(df$symbol,260),'Symbol6'=sample(df$symbol,280),
               'Symbol7'=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()

实战演练

# 读入数据
df = readFlie('./venn.txt',type = 'txt',row = F)
# 抽取数据,制造测试数据
set.seed(1234)
df_list = list('Symbol1'=sample(df$symbol,180),'Symbol2'=sample(df$symbol,200),
               'Symbol3'=sample(df$symbol,220),'Symbol4'=sample(df$symbol,240),
               'Symbol5'=sample(df$symbol,260),'Symbol6'=sample(df$symbol,280),
               'Symbol7'=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()
上一篇下一篇

猜你喜欢

热点阅读