交流群有一個(gè)昵稱是“今西”的小伙伴說(shuō)他做了一個(gè)常規(guī)的轉(zhuǎn)錄組分析,實(shí)驗(yàn)和對(duì)照兩個(gè)組,各三個(gè)樣本。他的老師希望能把一個(gè)基因集 里所有的基因(一個(gè)通路里面的,或者上下調(diào)最顯著的基因列表)都做箱線圖,表現(xiàn)在兩個(gè)組中的表達(dá)情況。一個(gè)基因集中的基因往往超過(guò)十個(gè),這次就不能像之前那樣一個(gè)個(gè)的畫圖拼接,必須要用循環(huán)做圖了。
但是循環(huán)的時(shí)候就報(bào)錯(cuò)了,他求助了他的師兄,發(fā)現(xiàn)問(wèn)題就出在aes() 上,搜索到:
https:///questions/29425892/how-do-i-loop-through-column-names-and-make-a-ggplot-scatteplot-for-each-one 如下所示:
You need to explicitly print () the object returned by ggplot() in a for loop because auto-print()ing is turned off there (and a few other places). You also need to use aes_string() in place of aes() because you aren't using i as the actual variable in filter(提問(wèn)者的dataframe) but as a character string containing the variable (in turn) in filter to be plotted.
讓我們來(lái)演示一下“今西”小伙伴遇到的問(wèn)題:
首先構(gòu)造模擬數(shù)據(jù)df = as.data.frame(rbind(matrix(rnorm(260 ),nrow = 10 ), matrix(rexp(260 ),nrow = 10 ) )) colnames(df)=paste0('gene' ,LETTERS) condition = rep(c('case' ,'control' ),each=10 )library (ggplot2) gencounts = as.data.frame(cbind(condition,df)) gencounts[1 :4 ,1 :4 ]
如下所示:
> gencounts[1:4,1:4] condition geneA geneB geneC 1 case -1.1783525 0.5862758 0.8885539 2 case 0.5305503 -0.2297670 0.1225264 3 case -1.9248160 1.7752445 -0.6351263 4 case 2.8276993 0.5792767 -1.6000259
總共是26個(gè)基因,它們都是在case 和 control兩個(gè)分組需要看表達(dá)量差異,而且case 和 control兩個(gè)分組內(nèi)部都是10個(gè)病人。
直接選取一個(gè)基因進(jìn)行繪圖# 任意一個(gè)基因繪圖 ggplot(gencounts, aes (x=condition, y= geneY)) + geom_boxplot(width=0.3,aes(fill=factor(condition)),show.legend = FALSE) + theme_bw()
如下所示:
選取一個(gè)基因進(jìn)行繪圖 因?yàn)橐獙懷h(huán),對(duì)全部的基因批量繪圖,所以“今西”小伙伴使用了如下所示的代碼:
i =2 colnames(gencounts)[i]# 不報(bào)錯(cuò),但是繪圖是失敗的后面的aes_string的差異 # 這里需要著重強(qiáng)調(diào) aes 和 if (T){ ggplot(gencounts, aes (x=condition, y= colnames(gencounts)[i])) + geom_boxplot(width=0.3,aes(fill=factor(condition)),show.legend = FALSE) + theme_bw() }
然后就報(bào)錯(cuò)了,如果需要讓上面的代碼成功,需要做一個(gè)簡(jiǎn)單的修改, 就是ggplot的aes和aes_string的差異:
# 成功的繪圖 ggplot(gencounts, aes_string (x='condition' , y= colnames(gencounts)[i])) + geom_boxplot(width=0.3,aes(fill=factor(condition)),show.legend = FALSE) + theme_bw()
測(cè)試后,就可以批量繪圖并且拼接啦;
pl = lapply(2:ncol(gencounts), function (i){ ggplot(gencounts, aes_string (x='condition' , y= colnames(gencounts)[i])) + geom_boxplot(width=0.3,aes(fill=factor(condition)),show.legend = FALSE) + theme_bw() })#cowplot::plot_grid(plotlist = pl, align = "h", nrow = 4) patchwork::wrap_plots(pl,byrow = T, nrow =7)
如下所示:
批量繪圖并且拼接 當(dāng)然了,這個(gè)箱線圖還可以進(jìn)一步美化, 比如添加統(tǒng)計(jì)學(xué)檢驗(yàn)指標(biāo):
# 箱線圖可以更加自定義,比如添加統(tǒng)計(jì)學(xué)顯著指標(biāo) i =2 colnames(gencounts)[i] ggplot(gencounts, aes_string (x='condition' , y= gencounts[,i]) ) + stat_boxplot(geom="errorbar" ,position=position_dodge(width=0.7 ),width=0.2 ,alpha=0.8 ,color="black" ) + geom_boxplot(width=0.3 ,aes(fill=factor(condition)),show.legend = FALSE ) + theme_bw()+ labs(y="Expression Level" ) + theme(panel.grid.major = element_blank(), #panel.grid.minor = element_blank(), panel.border = element_blank(), axis.line = element_line(colour = "black" ,size=1 ), axis.title.y=element_text(size=14 ),axis.text.y=element_text(size=14 ), axis.title.x=element_blank(),axis.text.x=element_text(size=14 ) ) + ggsignif::geom_signif(comparisons = list(c( 'case' ,'control' )), map_signif_level=T ,vjust=0.5 ,color="black" , textsize=5 ,test=t.test,step_increase=0.1 ) + scale_fill_manual(values=c("RoyalBlue" , "firebrick3" )) + ggtitle( colnames(gencounts)[i] )
如下所示:
添加統(tǒng)計(jì)學(xué)顯著指標(biāo) 該如何系統(tǒng)性學(xué)習(xí)ggplot呢如果你要從ggplot2開始一步步調(diào)制成為它這樣的美圖,需要下很深的功夫,一張統(tǒng)計(jì)圖就是從數(shù)據(jù)到幾何對(duì)象(點(diǎn)、線、條形等)的圖形屬性(顏色、形狀、大小等)的一個(gè)映射。
? 數(shù)據(jù)(Data),最基礎(chǔ)的是可視化的數(shù)據(jù)和一系列圖形映射(aesthetic mappings),該映射描述了數(shù)據(jù)中的變量如何映射到可見的圖形屬性。 ? 幾何對(duì)象(Geometric objects, geoms)代表在圖中實(shí)際看到的點(diǎn)、線、多邊形等。 ? 統(tǒng)計(jì)轉(zhuǎn)換(Statistical trassformations, stats)是對(duì)數(shù)據(jù)進(jìn)行某種匯總,例如將數(shù)據(jù)分組創(chuàng)建直方圖,或?qū)⒁粋€(gè)二維的關(guān)系用線性模型進(jìn)行解釋。 ? 標(biāo)度(Scales)是將數(shù)據(jù)的取值映射到圖形空間,例如用顏色、大小或形狀來(lái)表示不同的取值,展現(xiàn)標(biāo)度的常見做法是繪制圖例和坐標(biāo)軸。 ? 坐標(biāo)系(Coordinate system, coord)描述數(shù)據(jù)是如何映射到圖形所在的平面,同時(shí)提供看圖所需的坐標(biāo)軸和網(wǎng)格線。 ? 分面(faceting)如何將數(shù)據(jù)分解為子集,以及如何對(duì)子集作圖并展示。 ? 主題(theme)控制細(xì)節(jié)顯示,例如字體大小和圖形的背景色。 前面我們介紹了繪圖小白神包:
另外推薦5個(gè)ggplot2資源 ggplot2作者親自寫的書鏈接:https:///facet.html