对mRNA和lncRNA分别做富集分析,画横向柱状图,R代码
时间: 2024-03-23 21:41:28 浏览: 151
R语言绘制柱状图的代码
在R中,可以使用`ClusterProfiler`包进行基因富集分析,并使用`ggplot2`包绘制横向柱状图。以下是一个示例代码:
```R
# 安装和加载相应的R包
library(ClusterProfiler)
library(ggplot2)
# 读取差异表达结果和基因注释信息
res <- read.table("diff_expr_results.txt", header = TRUE)
anno <- read.table("gene_annotation.txt", header = TRUE)
# 提取显著差异的基因
sig_genes <- subset(res, padj < 0.05 & abs(log2FoldChange) > 1, select = "Gene")
# 进行mRNA和lncRNA的富集分析
mRNA_genes <- sig_genes[sig_genes$Gene %in% anno$Gene[anno$gene_type == "mRNA"], "Gene"]
lncRNA_genes <- sig_genes[sig_genes$Gene %in% anno$Gene[anno$gene_type == "lncRNA"], "Gene"]
mRNA_enrich <- enrichGO(mRNA_genes, OrgDb = "org.Hs.eg.db", ont = "BP", pvalueCutoff = 0.05, qvalueCutoff = 0.1, universe = anno$Gene[anno$gene_type == "mRNA"])
lncRNA_enrich <- enrichGO(lncRNA_genes, OrgDb = "org.Hs.eg.db", ont = "BP", pvalueCutoff = 0.05, qvalueCutoff = 0.1, universe = anno$Gene[anno$gene_type == "lncRNA"])
# 提取富集分析结果
mRNA_enrich_df <- as.data.frame(mRNA_enrich@result[,c("Description", "pvalue", "qvalue")])
lncRNA_enrich_df <- as.data.frame(lncRNA_enrich@result[,c("Description", "pvalue", "qvalue")])
# 绘制横向柱状图
mRNA_enrich_df$Category <- "mRNA"
lncRNA_enrich_df$Category <- "lncRNA"
enrich_df <- rbind(mRNA_enrich_df, lncRNA_enrich_df)
ggplot(enrich_df, aes(x = -log10(qvalue), y = reorder(Description, -log10(qvalue)), fill = Category)) +
geom_bar(stat = "identity", position = "dodge") +
xlab("-log10(qvalue)") + ylab("") +
theme(legend.position = "top", axis.text.y = element_text(size = 8)) +
scale_fill_manual(values = c("mRNA" = "blue", "lncRNA" = "red"))
```
在上述代码中,我们首先读取差异表达结果和基因注释信息,并根据转录本生物类型将显著差异的基因分别进行富集分析。然后,我们提取富集分析结果中的Description、pvalue和qvalue等信息,并将mRNA和lncRNA的富集分析结果合并。最后,我们使用`ggplot2`包绘制横向柱状图,将Description作为y轴,-log10(qvalue)作为x轴,并使用柱子的颜色区分mRNA和lncRNA。需要注意的是,实际应用中需要根据实验设计和研究目的进行相应的优化和调整。
阅读全文