有小伙伴發(fā)來圖蜀备,想讓實(shí)現(xiàn)一下,是一個(gè)KEGG分析的柱狀圖史汗,圖的特點(diǎn)是文字標(biāo)簽顏色與柱子顏色一樣琼掠,其實(shí)這個(gè)圖也就這么一個(gè)特點(diǎn),其他的柱狀圖的特征都沒有什么停撞,很普通的ggplot做法,最終效果如下:
image.png
首先讀入數(shù)據(jù)悼瓮,示例數(shù)據(jù)使用的是之前的文章:復(fù)現(xiàn)《nature communications》圖表(四):ggplot畫多組富集氣泡圖戈毒。
setwd("E:/")
A <- read.csv("GO.csv", header = T)
library(ggplot2)
library(forcats)
A$Description <- as.factor(A$Description)
A$Description <- fct_inorder(A$Description)
先做一個(gè)普通的柱狀圖。只需要兩個(gè)參數(shù)横堡,一個(gè)是gene count埋市,一個(gè)是富集的GO或者KEGG terms。
ggplot(A)+
geom_bar(aes(Description, Count),stat = "identity")+
coord_flip()
image.png
我們先不急著實(shí)現(xiàn)文字標(biāo)簽命贴,先進(jìn)行g(shù)gplot基礎(chǔ)的修飾道宅。
ggplot(A,aes(Description, Count))+
geom_bar(aes(fill=Cluster),stat = "identity")+
geom_text(aes(label=Count, y=Count+5),size=3)+
coord_flip()+
labs(x='',y='Gene count', title = 'GO enrichment of cluster')+
scale_fill_manual(values = c('#852f88',
'#eb990c',
'#0f8096'))+
theme_bw()+
theme(panel.grid = element_blank(),
legend.position = 'none',
axis.ticks.y = element_blank(),
plot.title = element_text(hjust = 0.5, size = 10))
image.png
接下來就是最后一步了,將文字標(biāo)簽顏色設(shè)置為和柱子一樣就完成了胸蛛。我這里是分為了3組污茵,將三組標(biāo)簽賦予對(duì)應(yīng)的顏色即可。
table(A$Cluster)
#Cluster1 Cluster2 Cluster3
#13 12 13
col <- c(rep("#852f88",13),rep("#eb990c",12),rep("#0f8096",13))
ggplot(A,aes(Description, Count))+
geom_bar(aes(fill=Cluster),stat = "identity")+
geom_text(aes(label=Count, y=Count+5),size=3)+
coord_flip()+
labs(x='',y='Gene count', title = 'GO enrichment of cluster')+
scale_fill_manual(values = c('#852f88',
'#eb990c',
'#0f8096'))+
theme_bw()+
theme(panel.grid = element_blank(),
legend.position = 'none',
axis.ticks.y = element_blank(),
plot.title = element_text(hjust = 0.5, size = 10),
axis.text.y = element_text(size=rel(0.85),colour =col),
plot.margin=unit(x=c(top.mar=0.2,right.mar=0.2,
bottom.mar=0.2,left.mar=0.2),
units="inches"))
image.png
還不錯(cuò)葬项,其他的細(xì)節(jié)問題這里就不再贅述了泞当,自行調(diào)整吧!
更多精彩內(nèi)容請(qǐng)關(guān)注我的公眾號(hào)=------《KS科研分享與服務(wù)》----------------------------