參考前文:R繪圖筆記 | R語言繪圖系統與常見繪圖函數及參數
關於繪圖圖,前面介紹了一些:
R繪圖筆記 | 一般的散點圖繪製
R繪圖筆記 | 柱狀圖繪製
R繪圖筆記 | 直方圖和核密度估計圖的繪製
R繪圖筆記 | 二維散點圖與統計直方圖組合
R繪圖筆記 | 散點分布圖與柱形分布圖
這裡介紹箱形圖的繪製,這些圖形在文章中是很常見的,也是必須要掌握的。比如下圖中的E圖(來自文獻:DOI: 10.1002/jcp.30015 )
一.讀入數據
如果你想獲取該數據用於自己練習,下面是獲取數據的地址:
https://docs.qq.com/sheet/DV0dxREV1YkJ0ZmVj
數據格式是這樣的。
數據第A列是病人ID,B~E列是臨床信息,其他列是病人的RNAseq數據。
你可以保存副本導出,然後自己讀入。
library(ggplot2)library(RColorBrewer)library(SuppDists)data <- read.csv("BioInfoNotesData1.csv",row.names = 1)假如我們需要繪製某基因在不同分期的表達情況。
f1.data <- data[,c(1,5)]colnames(f1.data) <- c("Stage","Value")summary(f1.data$Stage)summary(f1.data$Stage) N Stage I Stage II Stage III Stage IV 11 75 176 128 64先檢查數據是否有缺失值,分期信息不知用N來表示,可以刪除這些數據。
f1.data<-f1.data[f1.data$Stage!="N",]head(f1.data)BioinfoNotes>head(f1.data) Stage ValueTCGA-3L-AA1B-01 Stage I 7.04TCGA-4N-A93T-01 Stage III 7.23TCGA-4T-AA8H-01 Stage II 6.61TCGA-5M-AAT4-01 Stage IV 7.56TCGA-5M-AAT6-01 Stage IV 4.99TCGA-5M-AATE-01 Stage II 7.41二.繪圖
1.帶誤差線的箱形圖
繪製箱型圖用geom_boxplot函數。
geom_boxplot(mapping = NULL, data = NULL, stat = "boxplot", position = "dodge2", ..., outlier.colour = NULL, outlier.color = NULL, outlier.fill = NULL, outlier.shape = 19, outlier.size = 1.5, outlier.stroke = 0.5, outlier.alpha = NULL, notch = FALSE, notchwidth = 0.5, varwidth = FALSE, na.rm = FALSE, show.legend = NA, inherit.aes = TRUE)notch參數:如果為假(默認),則製作標準盒圖。如果為真,做一個缺口盒圖。凹槽用來比較組;如果兩個盒子的凹槽不重疊,說明中位數有顯著差異。
notchwidth參數:如果指定notch參數為TRUE的話,指定切口寬度,默認0.5。
varwidth參數:如果為假(默認),則製作標準盒圖。如果為真,那麼方框的寬度將與組中觀察值的平方根成正比。
ggplot(f1.data, aes(Stage, Value))+ geom_boxplot(aes(fill = Stage),notch = FALSE)+ scale_fill_manual(values=c(brewer.pal(8,"Set2")[c(3,6,7,8)]))+ theme_classic()+ ylab("The expression level")+ theme(panel.background=element_rect(fill="white",colour="black",size=0.25), axis.line=element_line(colour="black",size=0.25), axis.title=element_text(size=13,face="plain",color="black"), axis.text = element_text(size=12,face="plain",color="black"), legend.position="none" )
將notch改為TRUE。
ggplot(f1.data, aes(Stage, Value))+ geom_boxplot(aes(fill = Stage),notch = TRUE)+ scale_fill_manual(values=c(brewer.pal(8,"Set2")[c(3,6,7,8)]))+ theme_classic()+ ylab("The expression level")+ theme(panel.background=element_rect(fill="white",colour="black",size=0.25), axis.line=element_line(colour="black",size=0.25), axis.title=element_text(size=13,face="plain",color="black"), axis.text = element_text(size=12,face="plain",color="black"), legend.position="none" )
ggplot(f1.data, aes(Stage, Value))+ geom_boxplot(aes(fill = Stage),notch = TRUE,varwidth = T)+ scale_fill_manual(values=c(brewer.pal(8,"Set2")[c(3,6,7,8)]))+ theme_classic()+ ylab("The expression level")+ theme(panel.background=element_rect(fill="white",colour="black",size=0.25), axis.line=element_line(colour="black",size=0.25), axis.title=element_text(size=13,face="plain",color="black"), axis.text = element_text(size=12,face="plain",color="black"), legend.position="none" )
2.箱型與抖動散點組合圖
有時候,數據量大,我們需要顯示每一個數據點,可以很直觀的看到數據的分布情況。在文章中這種圖是很常見的。我們只需要在上面的箱形圖中加入geom_jitter函數就可以啦。
ggplot(f1.data, aes(Stage, Value))+ geom_boxplot(aes(fill = Stage))+ geom_jitter(binaxis = "y", position = position_jitter(0.3),stackdir = "center",dotsize = 0.4)+ scale_fill_manual(values=c(brewer.pal(7,"Set3")[c(1,3,5,7)]))+ theme_classic()+ labs(x='Stage',y='The expression level',title='Gene name')+ theme(panel.background=element_rect(fill="white",colour="black",size=0.25), axis.line=element_line(colour="black",size=0.25), axis.title=element_text(size=13,face="plain",color="black"), axis.text = element_text(size=12,face="plain",color="black"), legend.position="none" )
參考資料:
R語言數據可視化之美,張杰/著
geom_boxplot函數幫助文檔