怎么将得到的Go富集的结果进行可视化呢?本文使用ggplot2进行绘图。
首先我们先看一下上文GO富集得到的文件结果,这个结果是我进行处理过的,大家需要根据自己的列名将代码进行调整即可:

#首先加载包
library(reshape2)
library(ggplot2)
#读入富集结果文件
stat_frame <- read.delim(stat,head=TRUE,sep='\t')
#设置需要绘制的GO结果的数量,这里设置为10行,根据自己结果进行设置
bprows<-10
ccrows<-10
mfrows<-10
#以padj作为cutoff,对bp,cc,mf三部分进行筛选
bp <- subset(stat_frame,Category=='BP')[1:bprows,c('GeneRatio','Description','padj','Count','Category')]
cc <- subset(stat_frame,Category=='CC')[1:ccrows,c('GeneRatio','Description','padj','Count','Category')]
mf <- subset(stat_frame,Category=='MF')[1:mfrows,c('GeneRatio','Description','padj','Count','Category')]
提出符合条件的五列内容:'GeneRatio','Description','padj','Count','Category'
> head(bp)
GeneRatio Description padj Count Category
1 5/40 antigen processing and presentation 6.306981e-05 5 BP
2 8/40 immune response 1.647284e-04 8 BP
3 8/40 immune system process 1.647284e-04 8 BP
4 3/40 regulation of cell death 3.958135e-02 3 BP
5 3/40 regulation of apoptotic process 3.958135e-02 3 BP
6 3/40 regulation of programmed cell death 3.958135e-02 3 BP
#合并三部分内容
stat <- rbind(bp,cc,mf)
Description <- c()
Description <- as.character(stat$Description)
stat$Description <- factor(Description,levels=unique(Description))
绘制bar图形
p <- ggplot(stat,aes(x=Description,y=-log10(padj),fill=Category))+ geom_bar(stat='identity')+theme(axis.text.x=element_text(hjust=1,angle=45,size=6))
pdf(file='test.pdf',onefile=F)
p
dev.off()
结果如下:

绘制dot图形操作
#将GeneRatio以/作为分隔符,分为两列的矩阵
ratio <- matrix(as.numeric(unlist(strsplit(as.character(stat$GeneRatio),"/"))),ncol=2,byrow=TRUE)
> head(ratio)
[,1] [,2]
[1,] 5 40
[2,] 8 40
[3,] 8 40
[4,] 3 40
[5,] 3 40
[6,] 3 40
#第一列除以第二列
stat$GeneRatio <- ratio[,1]/ratio[,2]
> stat$GeneRatio
[1] 0.12500000 0.20000000 0.20000000 0.07500000 0.07500000 0.07500000
[7] 0.05000000 0.05000000 0.05000000 0.05000000 0.16666667 0.16666667
[13] 0.16666667 0.16666667 0.16666667 0.16666667 0.37500000 0.08333333
[19] 0.16666667 0.08333333 0.06451613 0.04838710 0.03225806 0.03225806
[25] 0.06451613 0.03225806 0.03225806 0.03225806 0.04838710 0.03225806
#根据padj值进行排序
stat <- stat[order(stat$padj,decreasing=F),]
#绘制基本图形
p <- ggplot(stat,aes(x=GeneRatio,y=Description,colour=padj,size=Count)) + geom_point()
#对点进行颜色改变,legend刻度的设置,文本颜色和大小设置
p <- p + scale_colour_gradientn(colours=rainbow(4),guide="colourbar") +
expand_limits(color=seq(0,1,by=0.25)) +
theme(axis.text=element_text(color="black",size=10))+
theme_bw() + theme(panel.border=element_rect(colour="black"))
pdf(file='dot.pdf')
p
dev.off()
最后dot.pdf结果如下:

网友评论