Python可视化科研信息学R语言训练

一文解决韦恩图(零代码版本、R语言、python版本)

2019-07-27  本文已影响75人  柳叶刀与小鼠标

(1)送给不喜欢编程的同学

http://bioinformatics.psb.ugent.be/webtools/Venn/

(1)导入准备做交集的文件。(2)点击submit选项。

其结果如下所示:



该网站可以完成最多30个数据集的交集绘制。其样式可以在上一步的output control修改。


(2)送给喜欢用Python的同学

# -*- coding: utf-8 -*-
"""
Created on Sat Jul 27 18:35:51 2019

@author: czh
"""
%reset -f
%clear
# In[*]
#绘图代码
from matplotlib import pyplot as plt
import numpy as np
from matplotlib_venn import venn3, venn3_circles
plt.figure(figsize=(4,4))
v = venn3(subsets=(230,32,109,33,56,20,44),set_labels = ('A', 'B', 'C'))
plt.show()
import matplotlib.pyplot as plt
from matplotlib_venn import venn3
import matplotlib.patheffects as path_effects



fig, ax = plt.subplots(figsize=(10, 10))
v = venn3(subsets = (10, 10, 4, 10, 4, 4, 2), set_labels = ('', '', ''), ax=ax)
v.get_label_by_id('100').set_text('Executive')
v.get_label_by_id('010').set_text('Legislative')
v.get_label_by_id('001').set_text('Judicial')
v.get_label_by_id('110').set_text('Example 1')
v.get_label_by_id('011').set_text('Example 2')
v.get_label_by_id('101').set_text('Example 3')
v.get_label_by_id('111').set_text('')
plt.title("The Three Branches of the US Government")

example_text = ('Example 1: The Vice President is considered "President of the Senate" and can vote to break ties.\n'
                'Example 2: The Legislature confirms Supreme Court justices.\n'
                'Example 3: The Executive appoints potential Supreme Court justices.')

text = fig.text(0.0, 0.05, example_text, ha='left', va='bottom', size=14)
text.set_path_effects([path_effects.Normal()])

plt.show()

python的限制比较明显,不能做三个以上数据集的交集,所以推荐用R语言来做。

(3)喜欢R语言的同学

setwd('D:\\F1\\deg')
rm(list=ls()) 
library(venneuler)
MyVenn <- venneuler(c(A=50,B=50,C=50,"A&B"=10,"A&C"=10,"B&C"=10,"A&B&C"=3))
MyVenn$labels <- c("","","",
                   "","",
                   "")

plot(MyVenn)

text(0.4,0.2,"A(n=60)", cex = 1)
text(0.4,0.8,"B(n=70)", cex = 1)
text(0.75,0.5,"C(n=50)", cex = 1)
text(0.5,0.5,"5", cex = 1)
text(0.4,0.5,"10", cex = 1)
text(0.55,0.4,"20", cex = 1)
text(0.5,0.6,"30", cex = 1)

这个代码从逻辑上看比较简单,仅仅在text上修改显示的内容即可。

library(VennDiagram)
A = 1:150
B = c(121:170,300:320)
C = c(20:40,141:200)
Length_A<-length(A)
Length_B<-length(B)
Length_C<-length(C)
Length_AB<-length(intersect(A,B))
Length_BC<-length(intersect(B,C))
Length_AC<-length(intersect(A,C))
Length_ABC<-length(intersect(intersect(A,B),C))

T<-venn.diagram(list(A=A,B=B),filename=NULL
                ,lwd=1,lty=2
                ,col=c('red','green'),fill=c('red','green')
                ,cat.col=c('red','green')
                ,rotation.degree=90)
grid.draw(T)


T<-venn.diagram(list(A=A,B=B,C=C),filename=NULL
                ,lwd=1,lty=2,col=c('red','green','blue')
                ,fill=c('red','green','blue')
                ,cat.col=c('red','green','blue')
                ,reverse=TRUE)
grid.draw(T)

(4)UpSetR包

正文

介绍一个R包UpSetR,专门用来集合可视化,更受杂志和编辑喜欢。

原理比较简单,做法大概分为两种,第一种是定义数据集后,画图自动取交集。第二种做法是先取交集,然后画图。绘制韦恩图的目的主要是查看数据集之间的异同。

(1)第一种:定义数据集后直接画图取交集

library(UpSetR)
library(dplyr)
library(tidyr)
rm(list=ls())
diff <- read.csv("diffSig_ttest.csv",header = T,row.names = 1)

加载包和所使用的数据。

AA <- subset(diff, splice_type=="AA")
AD  <- subset(diff, splice_type=="AD")
AP <- subset(diff, splice_type=="AP")
AT <- subset(diff, splice_type=="AT")

取出准备取交集的数据集们

#fromList
listinput <- list(AD = AD$symbol,
                  AP = AP$symbol,
                  AA = AA$symbol,
                  AT = AT$symbol)

library(UpSetR)
# pdf(file='upset.pdf',height = 8,width = 8)
p <- upset(fromList(listinput),nsets = 4, order.by = "freq")
# dev.off()

绘制图片

(2)取交集后在画图

setwd("E:\\Rwork")
library(UpSetR)
require(ggplot2);
require(plyr);
require(gridExtra); 
require(grid);
input <- c(
  'cancer1'=  1578,
  'cancer2' =  1284,
  'cancer3' = 2488,
  'cancer1&cancer2'  =205,
  'cancer1&cancer3'  = 828,
  'cancer2&cancer3'  =589,
  'cancer1&cancer2&cancer3'   =120
)

data <- fromExpression(input)
p1 <- upset(data, nsets = 9, 
            sets = c('cancer1',
                     'cancer2' ,
                     'cancer3'),
            keep.order = TRUE,
            # number.angles = 30, 
            point.size = 5, 
            line.size = 1.3, 
            mainbar.y.label = "IntersectionSize", 
            sets.x.label = "",
            mb.ratio = c(0.60, 0.40),
            text.scale = c(4, 4, 0.5, 0.5,3, 4))
p1
上一篇下一篇

猜你喜欢

热点阅读