用R語言進行GO與KEGG可視化分析
參考了CSDN這位大佬的代碼:
原文鏈接:https://blog.csdn.net/weixin_54004950/article/details/128498456
我視頻中自己使用的代碼放在下面:
#下載需要的R包
install.packages("ggplot2")
install.packages("openxlsx")
install.packages("tidyverse")
#加載需要的包
library (ggplot2)? #作圖?
library(tidyverse) #分列函數(shù)
library(openxlsx)? #打開excel
#工作目錄的確定
getwd()? #查詢工作目錄
setwd("C:/Users/LD/Desktop/GO&KEGG") #設(shè)定工作目錄(據(jù)自己需求)
#數(shù)據(jù)的讀取
BP = read.xlsx("C:/Users/LD/Desktop/GO&KEGG/GO&KEGG.xlsx",sheet= "BP")
CC = read.xlsx("C:/Users/LD/Desktop/GO&KEGG/GO&KEGG.xlsx",sheet= "CC")
MF = read.xlsx("C:/Users/LD/Desktop/GO&KEGG/GO&KEGG.xlsx",sheet= "MF")
dim(BP)
dim(CC)
dim(MF)
#數(shù)據(jù)中Term的分列
BP = separate(BP,Term, sep="~",into=c("ID","Term"))
CC = separate(CC,Term, sep="~",into=c("ID","Term"))
MF = separate(MF,Term, sep="~",into=c("ID","Term"))
#數(shù)據(jù)的整理
Go_data=data.frame(ID=c(BP$ID,CC$ID,MF$ID), #將ID整合
? ? ? ? ? ? ? ? ? ?Term=c(BP$Term,CC$Term,MF$Term), #將Term整合
? ? ? ? ? ? ? ? ? ?GeneNumber=c(BP$Count,CC$Count,MF$Count), #將GeneNumber整合
? ? ? ? ? ? ? ? ? ?Type=c(BP$Type,CC$Type,MF$Type))
#創(chuàng)建因子變量
Go_data$Type_order = factor(Go_data$Term,levels=Go_data$Term,ordered = T)
View(Go_data) #查看數(shù)據(jù)
ggplot(Go_data,??
? ? ? ?aes(x=Type_order,y=GeneNumber, fill=Type)) +? ? ? ? ? ? ? ?#根據(jù)type填充顏色
? geom_bar(stat="identity", width=0.9) +? ? ? ? ? ? ? ? ? ? ? ? ? #柱狀圖的寬
? scale_fill_manual(values = c("blue", "green", "red") ) +? ? ? ? #柱狀圖的填充顏色
? coord_flip() +? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? #縱向
? xlab("GO Term") +? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?#x軸的標(biāo)題
? ylab("Gene_Number") +? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?#y軸的標(biāo)題
? labs(title = "GO Enrich")+? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? #設(shè)置標(biāo)題
? theme_bw()? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? #設(shè)置主題
#氣泡圖
ago = rbind(BP,CC,MF)? #數(shù)據(jù)的集合
ago = as.data.frame(ago)
rownames(ago) = 1:nrow(ago)
# 創(chuàng)建因子變量
ago$order=factor(rev(as.integer(rownames(ago))),labels = rev(ago$Term))
#作圖
ggplot(ago,aes(y=order,x=Gene.ratio))+
? geom_point(aes(size=Count,color=PValue))+
? scale_color_gradient(low = "red",high ="blue")+
? labs(color=expression(PValue,size="Count"),
? x="Gene Ratio",y="GO term",title="GO Enrichment")+
? theme_bw()
#橫向
ggplot(Go_data,
? ? ? ?
? aes(x=Type_order,y=GeneNumber, fill=Type)) +? ? ? ? ? ? ?#x、y軸定義;根據(jù)Type填充顏色
? geom_bar(stat="identity", width=0.8) +? ? ? ? ? ? ? ? ? ?#柱狀圖的寬度
? scale_fill_manual(values = c("blue", "green", "red") ) + #柱狀圖的填充顏色
? xlab("GO term") +? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? #x軸標(biāo)題
? ylab("Gene_Number") +? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? #y軸標(biāo)題
? labs(title = "GO Terms Enrich")+? ? ? ? ? ? ? ? ? ? ? ? ?#設(shè)置標(biāo)題
? theme_bw()?
??
#KEGG可視化
?#數(shù)據(jù)的讀取
KEGG_data=read.xlsx("C:/Users/LD/Desktop/GO&KEGG/GO&KEGG.xlsx",sheet= "KEGG")
#作圖
ggplot(KEGG_data,aes(y=Term,x=Count,fill=PValue))+
geom_bar(stat = "identity",width=0.8)+ #柱狀圖寬度設(shè)置
??
? scale_fill_gradient(low = "red",high ="blue" )+
??
? labs(title = "KEGG Pathways Enrichment",? #設(shè)置標(biāo)題、x軸和Y軸名稱
? ? ? ?
? ? ? ?x = "Gene number",
? ? ? ?
? ? ? ?y = "Pathway")+
??
? theme(axis.title.x = element_text(face = "bold",size = 16),
? ? ? ??
? ? ? ? axis.title.y = element_text(face = "bold",size = 16),
? ? ? ??
? ? ? ? legend.title = element_text(face = "bold",size = 16))+
??
? theme_bw()
#KEGG氣泡圖
ggplot(KEGG_data,aes(y=Term,x=Gene.ratio))+
??
? geom_point(aes(size=Count,color=PValue))+
??
? scale_color_gradient(low = "red",high ="blue")+
??
? labs(color=expression(PValue,size="Count"),
? ? ? ?
? ? ? ?x="Gene Ratio",y="Pathways",title="KEGG Pathway Enrichment")+
??
? theme_bw()
視頻中使用的Excel表格文件與R.scipt分享至:https://www.jianguoyun.com/p/DaKIerIQ18DmCxj06JMFIAA
https://www.jianguoyun.com/p/DTpMJUgQ18DmCxj46JMFIAA