最近在基迪奧平臺上看到了這張KEGG富集信息圖,剛好手頭有批現(xiàn)成的轉(zhuǎn)錄組數(shù)據(jù),剛好繪制一下給富集圖換換風(fēng)格~~
image.png
準(zhǔn)備輸入文件
平臺上的輸入文件是直接可以用來出圖,這里前期我們需要對結(jié)果調(diào)整一下~輸入文件就只需要富集到的pathway及對應(yīng)的Count信息,再根據(jù)KEGG對應(yīng)關(guān)系將前倆層級的信息merge到一起即可。這里方法有挺多,可以自己寫script解析KEGG的pathway層級關(guān)系,也有一些R包或者在線網(wǎng)址可獲取,動手google。這里我只取每個Pathway1類別中p值從小到大排名前5的作為演示。
image.png
## 加載所需R包
sapply(c('ggplot2','RColorBrewer','tidyverse'), require, character.only = TRUE)
## 讀取KEGG富集結(jié)果文件
## 取pvalue前5的pathway
kegg_bar <- read.delim('../Data/test_keggBar.txt') %>% group_by(Pathway1) %>%
top_n(n = -5,wt = pvalue) ## 按照pvalue排名前5的pathway
## 取因子排名順序,后面用到
ft <- unique(kegg_bar$Pathway1)
增添表中信息
## pathway3列中增添pathway的信息,并設(shè)置Count為0
kegg_bar %>% distinct(Pathway1,.keep_all = T) %>% mutate(Pathway3 = Pathway1,Count = 0) -> a
## 合并為一起,按照Pathway和Count進行排序,并選取所需列
kegg_bar <- rbind(kegg_bar,a) %>% arrange(Pathway1,Count) %>%
select(Pathway1,Pathway3,Count,pvalue)
##添加一列Label,Count為0 的 標(biāo)記為空
kegg_bar$Label <- ifelse(kegg_bar$Count == 0," ",kegg_bar$Count)
# 按照之前預(yù)設(shè)的因子排序
kegg_bar$Pathway1 <- factor(kegg_bar$Pathway1,levels = ft)
kegg_bar <- kegg_bar[order(kegg_bar$Pathway1),]
## 設(shè)置因子水平
kegg_bar$Pathway3 <- kegg_bar$Pathway3 %>% factor() %>% fct_inorder() %>% fct_rev()
整理好的表格如下:
Pathway3中第一行為Pathway1大類,數(shù)值為0,Label為空。
image.png
作圖
p1 <- ggplot(kegg_bar,aes(x = Count,Pathway3,fill = Pathway1))+
geom_col()+
geom_text(aes(label =Label),size = 2.5,hjust = "left",nudge_x = 0.1)+ ## 添加標(biāo)簽
scale_x_continuous(limits = c(0,20.5),expand = expansion(mult = c(0,.1)))+ # 避免文字溢出
labs(x = "Number of Gene",y = "",title = "KEGG pathway anotation")+
scale_fill_brewer(palette = 'Set1')
p1
image.png
主題設(shè)置
首先獲取其中配色信息,將圖中的大類改為黑色,然后設(shè)置常規(guī)的主題信息即可。
## 復(fù)制
p2 <- p1
## 獲取配色
g <- ggplot_build(p2)
mycol <- g$data[[1]]["fill"]
col <- rev(mycol$fill)
## 將Aclass對應(yīng)的顏色改為黑色
num <- rev(kegg_bar$Count)
index <- which(num == 0)
col[index] <- "grey10"
## 自定義主題
my_theme <- theme_bw()+theme(plot.title = element_text(size = rel(1),hjust = 0.2,face = 'bold'),
axis.title = element_text(size = rel(1)),
axis.text.y = element_text(size = rel(0.85),
colour = col,face = 'bold'),
legend.position = "none",
plot.margin = unit(x = c(top.mar = 0.2,
right.mar = 0.2,
left.mar=0.2,
bottom.mar= 0.2),units = 'inches'))
plot <- p2+my_theme1
plot
ggsave('./KEGG_bar.pdf', plot, width = 8, height = 10)
image.png