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()