画个GO/KEGG富集图有多简单

2024-06-29  本文已影响0人  生信云笔记

  clusterProfiler包做富集分析后,可直接使用配套的可视化函数展示结果,最常见的形式有barplotdotplot。网上有很多相关的推文,今天咱来说点不一样的方式,讲讲如何利用barplot快速画出富集条形图以及如何扩展到其他数据。

library(clusterProfiler)
library(ggplot2)

data(geneList, package = "DOSE")
de <- names(geneList)[1:100]
yy <- enrichGO(de, 'org.Hs.eg.db', ont="BP", pvalueCutoff=0.01)
p <- barplot(yy, showCategory=10)
p

  clusterProfiler富集返回的结果是enrichResult对象,可以直接使用barplot函数绘制条形图,来自enrichplot包的函数,实际调用的是barplot.enrichResult。不过,生成的图可能并不够简约,也许你想要横坐标直接展示显著性即可,不要基因数量。

yy@result$Count <- -log10(yy@result$p.adjust)
p <- barplot(yy, showCategory=10) + scale_fill_continuous(low="red", high="red", name='', guide=F)
p

  barplot默认分别使用CountDescription两个变量来画横坐标和纵坐标,这里用-log10转换后的p.adjust替换原来的Count值来画横坐标。默认p.adjust映射为颜色深浅,想要去除可以重新映射一下,并移除图例。如此,便可以得到想要的富集图。

  做富集分析的软件那么多,如果没有使用clusterProfiler做分析,如何借助barplot画图呢?

df <- read.table('sample_go.txt', header=T, sep='\t', stringsAsFactors=F)
head(df)
                   id                                                desc
GO:0044784 GO:0044784         metaphase/anaphase transition of cell cycle
GO:0010965 GO:0010965   regulation of mitotic sister chromatid separation
GO:0051306 GO:0051306                 mitotic sister chromatid separation
GO:1905818 GO:1905818                 regulation of chromosome separation
GO:0033047 GO:0033047  regulation of mitotic sister chromatid segregation
GO:0030071 GO:0030071 regulation of mitotic metaphase/anaphase transition
                    adj
GO:0044784 3.692440e-11
GO:0010965 4.179973e-11
GO:0051306 6.779593e-11
GO:1905818 9.052142e-11
GO:0033047 2.259322e-10
GO:0030071 4.991328e-10

colnames(df)[2:3] <- c('Description', 'p.adjust')
df$GeneRatio <- 0
df$Count <- -log10(df$p.adjust)

res <- new("enrichResult", result=df)
p <- barplot(res, showCategory=10) + scale_fill_continuous(low="red", high="red", name='', guide=F)
p$layers[[1]]$geom_params$width <- 0.6
p

  自定义的数据结果可以构建成barplot需要的输入对象来画图,生成的是ggplot对象,修改起来也是相当方便。

上一篇 下一篇

猜你喜欢

热点阅读