看到别人文章里面的GO和KEGG富集分析结果,画的一张图有通路有基因,于是,懒惰的本小卡拉弄了一个R脚本,满足自己的小好胜心!
一、文件准备
-
GO/KEGG富集分析结果文件,格式如下:
-
差异表达基因文件,格式如下:
二、R代码
getwd()
rm=list(ls)
getwd()
setwd("D:/Rcicrcleplot")
library(ggplot2)
library(circlize)
library(GOplot)
library(tidyverse)
library(dplyr)
list.files()
go<-read.csv("GO.csv",header=T,sep=",")
View(go)
DEG<-read.csv("DEG.csv",header=T,sep=",")
View(DEG)
go<- go[,c("ID","Count","Description","ONTOLOGY","pvalue","geneID")]
View(go)
pro<-DEG[,c("geneSymbol","log2FoldChange")]
View(pro)
go$GeneList<-gsub("/",",",go$geneID)
View(go)
go<- go[,c("ID","Count","Description","ONTOLOGY","pvalue","GeneList")]
View(go)
pro<-DEG[,c("geneSymbol","log2FoldChange")]
View(pro)
colnames(go)<-c( 'ID','ObservedGeneNum', 'term','category','adj_pval','genes')
View(go)
colnames(pro)<-c("ID","logFC")
View(pro)
#pro <- pro %>% filter(logFC > log2(1.2) | logFC < -log2(1.2))
#View(pro)
circ <- circle_dat(go,pro)
View(circ)
#write.table(circ,file="circ.xls",col.names=T,row.names=T,quote=F,sep="\t")
#head(go)
go <- go[order(go$ObservedGeneNum), ]
View(go)
gop<-go[1:5,]
gop <- go
View(gop)
###展示基因与GO Terms关系的圈图 (GOChord())###
chord <-chord_dat(data = circ, genes = pro,process = gop$term)
#write.table(chord,file="chord.xls",col.names=T,row.names=T,quote=F,sep="\t")
cols<- gop$term
pdf("cicrplot.pdf",width=15,height=16)
#GOChord(data, title, space, gene.order, gene.size,gene.space, nlfc = 1,lfc.col, lfc.min,lfc.max, ribbon.col, border.size, process.label, limit)
#data: 二进制矩阵
#title:标题
#space:基因对应方块之间的距离
#gene.order:基因排列顺序
#gene.size:基因标签大小
#nlfc:logFC 列的数目
#lfc.col:LFC颜色,定义模式:c(color for low values, color for the mid point, color for the highvalues)
#lfc.min:LFC最小值
#lfc.max:LFC最大值
#ribbon.col:向量定义基因与GO Term间条带颜色
#border.size:基因与GO Term间条带边框粗细
#process.label:GO Term 图例文字大小
#limit:c(3, 2),两个数字;第一个参数筛选基因(保留至少存在于3个GO Term的基因),第二个参数筛选GO Term(保留至少包含2个基因的GO Term )
GOChord(chord,
space = 0.02,
gene.order = 'logFC',
gene.space = 0.25,
#border.size = NA ,
gene.size = 5,
ribbon.col=brewer.pal(5, "Set3"),
#process.label = 15
) +
guides(size = guide_legend("GO Terms", ncol = 2, byrow = T,
override.aes = list(shape = 22, fill = brewer.pal(5, "Set3"),
size = 8)))
dev.off()
###泡图(GOBubble())###
pdf("GOBubbleplot.pdf",width=15,height=16)
GOBubble(circ, labels = 3)
dev.off()
pdf("GOBubble_multipleplot_.pdf",width=15,height=16)
GOBubble(circ,
#title = 'Bubble plot',
colour =c('orange', 'darkred', 'gold'),
display = 'multiple',
labels = 3)
dev.off()
### 圈图展示基因富集分析结果(GOCircle())###
pdf("GOCircleplot_.pdf",width=10,height=11)
GOCircle(circ)
dev.off()
###基因与GO Term的热图(GOHeat) ###
#nlfc = 1:颜色对应l
#ogFC nlfc = 0:颜色对应每个基因注释了到了几个GO Term
pdf("GOHeat_plot_.pdf",width=8.5,height=8)
GOHeat(chord, nlfc = 1, fill.col = c('red', 'yellow','green'))
dev.off()
###聚类(GOCluster)###
#GOCluster()调用R内置函数hclust 根据功能分内进行层次聚类。
pdf("GOClusterterm_plot_.pdf",width=11,height=11)
GOCluster(circ,
gop$term,
clust.by = 'logFC',
term.width = 2,
term.col = brewer.pal(length(gop$term), "Set3"))+
guides(size = guide_legend("GO Terms", ncol = 2, byrow = T,
override.aes = list(shape = 22,
fill = brewer.pal(length(gop$term), "Set3"),
size = 8)))
dev.off()
三、图(很多种,这里随便挑几个展示)
自己研究的小代码,仅供相互交流学习,请勿用于商业用途,未经允许,禁止转载,谢谢!