最近,,看到一篇nature文章,,整篇文章的圖的風(fēng)格類似于如下圖1,,因為第一次見這種圖,,感覺展示效果不錯,,所以嘗試做一下,!Fluhr, L., Mor, U., Kolodziejczyk, A.A. et al. Gut microbiota modulates weight gain in mice after discontinued smoke exposure,,Nature,2021.本來看著Figure1 h很簡單,,就先復(fù)現(xiàn)它了,,結(jié)果我錯了,它真的太麻煩了,,網(wǎng)上關(guān)于ggplot做多組柱狀圖深入修飾的教程也比較少,。直到最后,我也沒有完全復(fù)現(xiàn)它的樣子,。問題是:如何改變分組的填充,,但是點(diǎn)的顏色不變!
(我復(fù)現(xiàn)的圖,,有9分相似)
這篇Nature文章提供了作圖數(shù)據(jù),,在附件Source Data Fig. 1,,有Figure1作圖需要的所有數(shù)據(jù)。首先我們讀入數(shù)據(jù),,并通過因子設(shè)置變量順序與原文一致,!setwd("F:/生物信息學(xué)/NATURE折線圖") A <- read.csv("A.csv",header = T) library(ggplot2) library(forcats) A$condictions <- as.factor(A$condictions) A$condictions <- fct_inorder(A$condictions) A$group <- as.factor(A$group) A$group <- fct_inorder(A$group)
接著就比較直接了,畫柱狀圖,。
ggplot(A, aes(fill=condictions, y=values, x=group))+ geom_bar(position=position_dodge(),stat="summary",colour = "black",size=1)+ theme_classic(base_size = 12)
這個柱狀圖就是普通的柱狀圖,,到這一步,我們需要思考下,,一步一步先解決這幾個問題,,柱狀圖坐落在坐標(biāo)上,柱子之間有空隙,,改變legend位置為水平,,設(shè)置x,y軸標(biāo)題,。 ggplot(A, aes(fill=condictions, y=values, x=group))+ geom_bar(position=position_dodge(1),stat="summary",width=0.7,colour = "black",size=1)+ theme_classic(base_size = 12)+ theme(legend.direction = "horizontal", legend.position = "top")+ labs(title = "", y="Fecal calories per g", x = "")+ scale_y_continuous(limits = c(0,6000),expand = c(0,0))+ theme(axis.text.x = element_text(size = 12))+ theme(axis.text.y = element_text(size = 12))+ theme(axis.title = element_text(size = 14))
然后就是這幅圖比較特色的地方了,,添加分割線,右側(cè)有填充,,本來是一個簡單的事情,,但是由于橫坐標(biāo)不是數(shù)字,而是因子,,所以這里有點(diǎn)區(qū)別,。
ggplot(A, aes(fill=condictions, y=values, x=group))+ geom_bar(position=position_dodge(1),stat="summary",width=0.7,colour = "black",size=1)+ theme_classic(base_size = 12)+ geom_vline(aes(xintercept=as.numeric(as.factor(group))+0.5),linetype=2,cex=1.2)+ geom_rect(aes(xmin=as.numeric(as.factor(group))+0.5, xmax=Inf, ymin=(-Inf),ymax=Inf), fill='grey90',color='grey90')+ geom_vline(xintercept =A$condictions,linetype=2,cex=1.2)+ geom_bar(position=position_dodge(1),stat="summary",width=0.7,colour = "black",size=1)+ theme(legend.direction = "horizontal", legend.position = "top")+ labs(title = "", y="Fecal calories per g", x = "")+ scale_y_continuous(limits = c(0,6000),expand = c(0,0))+ theme(axis.text.x = element_text(size = 12))+ theme(axis.text.y = element_text(size = 12))+ theme(axis.title = element_text(size = 14))
圖的主體已經(jīng)出來了,接著添加誤差線,,我這里選擇了stat_summary函數(shù),,這個方法比較簡單,但是也有自己計算添加的,,比較麻煩,。這里需要注意的是添加誤差線的位置!
ggplot(A, aes(fill=condictions, y=values, x=group))+ geom_bar(position=position_dodge(1),stat="summary",width=0.7,colour = "black",size=1)+ theme_classic(base_size = 12)+ geom_vline(aes(xintercept=as.numeric(as.factor(group))+0.5),linetype=2,cex=1.2)+ geom_rect(aes(xmin=as.numeric(as.factor(group))+0.5, xmax=Inf, ymin=(-Inf),ymax=Inf), fill='grey90',color='grey90')+ geom_vline(xintercept =A$condictions,linetype=2,cex=1.2)+ geom_bar(position=position_dodge(1),stat="summary",width=0.7,colour = "black",size=1)+ stat_summary(fun.data = 'mean_se', geom = "errorbar", colour = "black", width = 0.2,position = position_dodge(1))+ theme(legend.direction = "horizontal", legend.position = "top")+ labs(title = "", y="Fecal calories per g", x = "")+ scale_y_continuous(limits = c(0,6000),expand = c(0,0))+ theme(axis.text.x = element_text(size = 12))+ theme(axis.text.y = element_text(size = 12))+ theme(axis.title = element_text(size = 14))
最后就是比較難的內(nèi)容了,,包括添加散點(diǎn),,添加顯著性!到這里真的是做了一天了,,但是還是有一個問題沒有解決,,不能像原文那樣,,柱子為白色,,點(diǎn)為自己的顏色,不知道有沒有簡單的方法,! ggplot(A, aes(fill=condictions, y=values, x=group))+ geom_bar(position=position_dodge(1),stat="summary",width=0.7,colour = "black",size=1)+ theme_classic(base_size = 12)+ geom_vline(aes(xintercept=as.numeric(as.factor(group))+0.5),linetype=2,cex=1.2)+ geom_rect(aes(xmin=as.numeric(as.factor(group))+0.5, xmax=Inf, ymin=(-Inf),ymax=Inf), fill='grey90',color='grey90')+ geom_vline(xintercept =A$condictions,linetype=2,cex=1.2)+ geom_bar(position=position_dodge(1),stat="summary",width=0.7,colour = "black",size=1)+ stat_summary(fun.data = 'mean_se', geom = "errorbar", colour = "black", width = 0.2,position = position_dodge(1))+ theme(legend.direction = "horizontal", legend.position = "top")+ labs(title = "", y="Fecal calories per g", x = "")+ scale_y_continuous(limits = c(0,6000),expand = c(0,0))+ theme(axis.text.x = element_text(size = 12))+ theme(axis.text.y = element_text(size = 12))+ theme(axis.title = element_text(size = 14))+ geom_jitter(data = A, aes(y = values),size = 3, shape = 21, stroke = 0.01, show.legend = FALSE, position = position_jitterdodge(jitter.height=1,dodge.width = 1))+ geom_signif(y_position=c(5000,5000,5000,5500), xmin=c(0.6,1.1,1.6,1.9), xmax=c(0.8,1.3,1.8,2.3), annotation=c("**","**","****","****"), tip_length=0, size=0.8, textsize = 7, vjust = 0.3)+ scale_fill_manual(values = c('#5494cc','#e18283','#0d898a','#f9cc52'))
太累了,,感覺還是用GraphPad做吧,!哈哈哈,不過做出來了還是很得意的,!看在這么辛苦的份上,,還不點(diǎn)贊,給點(diǎn)賞賜,?,?? 最后,,需要示例數(shù)據(jù)和完整代碼注釋的小伙伴打賞截圖,,發(fā)送公眾號,并留下郵箱,,我們雙手奉上,!感謝!
|