R可視化:KEGG富集分析可視化

  1. 加載R包
library(DOSE)
library(clusterProfiler)
library(org.Hs.eg.db)
library(stringr)
library(forcats)
library(ggplot2)
library(ggh4x)
library(RColorBrewer)
library(tidyverse)
library(ggthemes)
library(ggsci)
  1. 加載數(shù)據集
data(geneList)
gene <- names(geneList)[1:2000]
  1. KEGG富集分析
eK <- enrichKEGG(gene         = gene,
                 organism     = 'hsa',
                 pvalueCutoff = 0.05,
                 qvalueCutoff = 0.05)
  1. 結果轉換
eK_res <- as.data.frame(eK)
  1. 選取前20項進行可視化
eK_data <- eK_res[1:min(20, nrow(eK_res)), ] 
  1. 給各通路加上對應map號
eK_data$label <- sprintf("%s\n(%s)", eK_data$Description, eK_data$ID)
  1. ggplot2柱狀圖可視化
#轉換成因子
eK_data1 <- mutate(eK_data, label = factor(label, levels = unique(label)))

#計算富集因子
eK_data1 <- eK_data1 %>%
  separate(GeneRatio, into = c("GeneHitsInSelectedSet", 
                               "AllGenesInSelectedSet"), sep = "/") %>%
  separate(BgRatio, into = c("GeneHitsInBackground", 
                             "AllGenesInBackground"), sep = "/") %>%
  mutate(across(c(GeneHitsInSelectedSet,AllGenesInSelectedSet, 
                  GeneHitsInBackground, AllGenesInBackground), as.numeric)) %>%
  mutate(enrichFactor = (GeneHitsInSelectedSet * AllGenesInBackground) / 
           (AllGenesInSelectedSet * GeneHitsInBackground))
#ggplot2可視化:按顯著性排序的柱狀圖
eK_bar <- 
  ggplot(eK_data1, aes(x = Count, y = label, fill = p.adjust)) +
  geom_bar(stat = "identity", color = "black", linewidth = 0.4, alpha = 0.8,
           width = 0.75) +
  scale_x_continuous(expand = c(0.025,0)) +
  #scale_fill_viridis_c(option = "mako") +
  scale_fill_gradient(low="white",high="#d73026",name="p.adjust") +
  labs(x = "Count", y= NULL) +
  theme_minimal(base_size = 14) +
  theme(axis.text = element_text(color = "black")) +
  theme(panel.grid.minor = element_blank(),
        panel.grid.major = element_blank()) +
  theme(axis.line.x = element_line(color = "black", linewidth = 0.35),
        axis.ticks.x = element_line(color = "black", linewidth = 0.35),
        axis.ticks.length = unit(0.3,"cm")) +
  guides(x = "axis_truncated", y = "axis_truncated")
eK_bar
按顯著性排序的柱狀圖.png
  1. ggplot2氣泡圖可視化
#ggplot2可視化:按顯著性和富集因子排序的氣泡圖
eK_dot <-
ggplot(eK_data1,aes(x = enrichFactor, y = label)) + 
  geom_point(aes(size = Count, fill = p.adjust),
             alpha = 1,
             shape = 21,
             stroke=0.5,
  ) + 
  scale_x_continuous(expand = c(0.05,0)) +
  scale_fill_gradient(low="white",high="#e1415f",name="p.adjust") +
  labs(color=expression(p.adjust),
       size="Count",
       x="enrichFactor",
       y=" ") +
  guides(fill = NULL,
         size = guide_legend(order=1)) +
  theme_linedraw(base_size = 14) +
  theme(axis.text = element_text(color="black"),
        axis.ticks.length = unit(0.25,"cm")) +
  theme(panel.grid.minor = element_blank(),
        panel.grid.major = element_line(color = "black", linewidth = 0.25),
        panel.border = element_rect(fill=NA,linewidth = 1),
        legend.background = element_blank()
  )
eK_dot
富集氣泡圖.png
  1. ggplot2分類柱狀圖可視化
#按照分類重新排序
eK_data2 <- eK_data %>%
  mutate(label = fct_reorder(label, category))

#ggplot2可視化:按分類排序的柱狀圖
eK_col <-
  ggplot(eK_data2, aes(x = Count,
                y = label,
                fill = category))+
  geom_bar(stat="identity",color = "black", linewidth = 0.4, 
           alpha = 0.8,width = 0.75,
           key_glyph = "point") +
  labs(x = NULL, y= NULL,
       fill = "Category") +
  scale_x_continuous(expand = c(0.025,0)) +
  scale_fill_viridis_d() +
  theme_minimal(base_size = 13) +
  theme(axis.text = element_text(color = "black")) +
  theme(panel.grid.minor = element_blank(),
        panel.grid.major = element_blank()) +
  theme(axis.line.x = element_line(color = "black", linewidth = 0.35),
        axis.ticks.x = element_line(color = "black", linewidth = 0.35),
        axis.ticks.length = unit(0.3,"cm")) +
  guides(x = "axis_truncated", y = "axis_truncated") +
  guides(fill = guide_legend(override.aes = list(shape = 21, size = 5,
                                                 color = "white")))
eK_col
按分類排序的柱狀圖.png
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容