2020-02-15

富集分析制作气泡图

###自定义作气泡图

x<-read.csv(file.choose(),stringsAsFactors = F)

#筛选p<0.05

x<-x[x$PValue<0.05,]

x_go=x[,1:5]

xbp=x_go[grep("BP",x_go$Category),]

xcc=x_go[grep("CC",x_go$Category),]

xmf=x_go[grep("MF",x_go$Category),]

xkegg=x_go[grep("KEGG",x_go$Category),]

xbp$Term=gsub(".*\\~","",xbp$Term)#Biological Process

xcc$Term=gsub(".*\\~","",xcc$Term)#Cell Component

xmf$Term=gsub(".*\\~","",xmf$Term)#Molecular Function

xkegg$Term=gsub(".*\\:","",xkegg$Term)#KEGG pathway

#加载ggplot2

library(ggplot2)

make_GO_bubble<-function(go_data,term_name){


  #选择top10的数据(count)

  GO_DATA=go_data[order(go_data$Count,decreasing = T),]

  GO_DATA=head(GO_DATA,10)


  # 四维数据的展示

  p = ggplot(GO_DATA,aes(X.,Term))

  bubble=p+ geom_point(aes(size=Count,color=-log10(PValue)))


  # 自定义渐变颜色

  bubble =bubble+ scale_colour_gradient(low="green",high="red")


  # 改变图片的样式(主题)

  pr=bubble + theme_test(base_size = 16,base_rect_size = 1)


  pr=pr+labs(x="Rich factor",y=term_name,title="Enrichment of DEGs")


  return(pr) } 

#BP

make_GO_bubble(xbp,term_name="Biological Process")#HEIGHT 550

make_GO_bubble(xcc,term_name = "Cell Component")

make_GO_bubble(xmf,term_name = "Molecular Function")

make_GO_bubble(xkegg,term_name = "KEGG pathway")

你可能感兴趣的:(2020-02-15)