GO富集结果绘图(TBtools进行GO富集分析)

该文介绍了如何在R环境中进行GO富集分析,包括读取数据、数据整理和筛选,特别是计算GeneRatio和BgRatio,以及基于这些数据绘制柱状图和气泡图来展示结果。文中使用的工具有dplyr和ggplot2等。
摘要由CSDN通过智能技术生成

1、不太确定BgRatio应该怎样计算,大家如果明白的话麻烦告诉我。

2、一共两种图,

#重开
rm(list = ls())
setwd("D:/great_work/R_wd/GO&KEGG_plot/")

#富集
#GO富集
#KEGG富集
#GSEA富集分析

#导入数据
library(dplyr)
data <- read.table("GO.Enrichment.final.txt", sep="\t", head = T, check.names=F)

#整理数据,排序和筛选
#有时需要取前10的结果
# sorted_data <- data %>%
#   arrange(Class, .data[["corrected p-value(BH method)"]]) %>%
#   group_by(Class) %>%
#   filter(`corrected p-value(BH method)`<0.05) %>%
#   slice_head(n = 10)

CCindex<-grep("Cellular component",data$Class)
BPindex<-grep("Biological process",data$Class)
MFindex<-grep("Molecular function",data$Class)

CC<-data[CCindex,]%>%mutate(Description = reorder(GO_Name,`corrected p-value(BH method)`))%>%filter(`corrected p-value(BH method)`<0.05)
BP<-data[BPindex,]%>%mutate(Description = reorder(GO_Name,`corrected p-value(BH method)`))%>%filter(`corrected p-value(BH method)`<0.05)
MF<-data[MFindex,]%>%mutate(Description = reorder(GO_Name,`corrected p-value(BH method)`))%>%filter(`corrected p-value(BH method)`<0.05)

CC$GeneRatio = CC$HitsGenesCountsInSelectedSet/ CC$AllGenesCountsInSelectedSet
BP$GeneRatio = BP$HitsGenesCountsInSelectedSet/ BP$AllGenesCountsInSelectedSet
MF$GeneRatio = MF$HitsGenesCountsInSelectedSet/ MF$AllGenesCountsInSelectedSet

CC$BgRatio = CC$HitsGenesCountsInSelectedSet/ CC$AllGenesCountsInBackground
BP$BgRatio = BP$HitsGenesCountsInSelectedSet/ BP$AllGenesCountsInBackground
MF$BgRatio = MF$HitsGenesCountsInSelectedSet/ MF$AllGenesCountsInBackground

data2 <- rbind(BP, CC, MF)

#设置展示GO条目的顺序
Description <- c()
Description <- as.character(data2$GO_Name)
data2$GO_Name <- factor(Description,levels=unique(Description))
#------------------------------------------------------------------------------#
#1绘图-柱形图-三颜色
library(RColorBrewer)
#display.brewer.all()#展示所有配色方案
color <- brewer.pal(3,"Set2")

library(ggplot2)
pdf(file="WGCNA_pinkGO1_plot.pdf", width = 12, height = 8)
ggplot(data2) +
  aes(x = GO_Name, y = -log10(`corrected p-value(BH method)`), fill = Class) +
  geom_bar(stat = "identity", color = "black") +#使用xy数值作为bar的高度
  scale_fill_manual(values = color) +#颜色渐变,使用配色方案
  labs(y = expression(-log[10](p.adj))) +#额外设置y轴标签,否则报错。下标。
  theme(
    axis.title = element_text(size = 15, face = "plain", color = "black"),
    axis.text = element_text(size = 12, face = "plain", color = "black"),
    axis.text.x = element_text(angle = 90, color = "black", hjust = 1, vjust = 0.6),
    axis.title.x = element_blank(),
    legend.title = element_blank(),
    legend.text = element_text(size = 8, face = "bold"),
    legend.margin = margin(t = 0, r = 0, b = 0, l = 0, unit = "pt"),
    legend.direction = "horizontal",
    legend.position = c(0.8, 0.9),
    legend.background = element_blank(),
    panel.background = element_rect(fill = "transparent", color = "black"),
    plot.background = element_blank()
  )

dev.off()

#------------------------------------------------------------------------------#
#2绘图-气泡图-GeneRatio为横坐标-颜色为p.adj
library(ggplot2)
pdf(file="WGCNA_pinkGO2_plot.pdf", width = 12, height = 8)

ggplot(data2)+
  aes(x = GeneRatio, y = GO_Name, colour = -1*log10(`corrected p-value(BH method)`), size = HitsGenesCountsInSelectedSet)+
  geom_point()+
  #图片背景设定
  theme(axis.title=element_text(face="bold", size=14,colour = 'black'), #坐标轴标题
        axis.text=element_text(face="bold", size=14,colour = 'black'), #坐标轴标签
        axis.line = element_line(size=0.5, colour = 'black'), #轴线
        panel.background = element_rect(color='black'), #绘图区边框
        legend.key = element_blank() #关闭图例边框
  )+
  scale_size(range=c(2, 8))+
  scale_colour_gradient(low = "green", high = "red")+
  theme_bw()+
  ylab("GO Terms")+
  xlab("GeneRatio")+
  labs(color=expression(-log[10](p.adj)))+#使用下标形式
  theme(legend.title=element_text(size=14), legend.text = element_text(size=14))+
  theme(axis.title.y = element_text(margin = margin(r = 50)), axis.title.x = element_text(margin = margin(t = 20)))+
  theme(axis.text.x = element_text(face ="bold", color="black", angle=0, vjust=1))

dev.off()

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值