在「R作圖」專輯中,我們將向大家介紹常見圖形的R作圖方法以及對應代碼解釋,幫助大家輕鬆理解和學習R作圖技巧。
這一講,我們將介紹如何用ggplot2軟體包中的函數geom_boxplot()繪製複雜箱式圖。
函數geom_boxplot()簡化格式為:
geom_boxplot(outlier.colour="black", outlier.shape=16, outlier.size=2, notch=FALSE)·Outlier.colour, outlier.shape, outlier.size:分別表示離群點的顏色,形狀和大小。
·notch:邏輯值。如果為TRUE,則繪製一個缺口箱式圖。缺口在中位數及其置信區間,該置信區間通常基於中位數+/- 1.58 * IQR / sqrt(n)。缺口用於比較組;如果兩個箱式的缺口不重疊,則有力證明中位數不同。
使用R內置的ToothGrowth數據集:
ToothGrowth$dose<-as.factor(ToothGrowth$dose)head(ToothGrowth)輸出結果
len supp dose 1 4.2 VC 0.5 2 11.5 VC 0.5 3 7.3 VC 0.5 4 5.8 VC 0.5 5 6.4 VC 0.5 6 10.0 VC 0.5確保使用上述R腳本將變量劑量轉換為因子變量。
library(ggplot2)# 基本箱式圖,將基本箱式圖保存在p中p<-ggplot(ToothGrowth, aes(x=dose, y=len))+ geom_boxplot() #表示對ggplot()函數中繪圖內容進行geom_boxplot()函數運算p #列印基本箱式圖,即輸入p# 轉置箱式圖p+coord_flip()# 繪製缺口箱式圖ggplot(ToothGrowth, aes(x=dose, y=len))+ geom_boxplot(notch=TRUE)# 更改離群值的顏色、形狀和大小ggplot(ToothGrowth, aes(x=dose, y=len))+ geom_boxplot(outlier.colour="red", outlier.shape=8, outlier.size=4)
函數stat_summary()可用於向箱式圖添加均值:
# 添加箱式圖的均值p+stat_summary(fun.y=mean, geom="point", shape=23, size=4)
選擇要顯示的項目:僅顯示x軸dose為0.5和2的內容
p+scale_x_discrete(limits=c("0.5", "2"))
可以使用函數geom_dotplot()或geom_jitter()將點添加到箱式圖中:
p+geom_dotplot(binaxis='y', stackdir='center', dotsize=1)p+geom_jitter(shape=16, position=position_jitter(0.2))
箱式圖線顏色可以通過dose的水平改變:
# 根據組別自動更改框線顏色:color=dose,顏色不可選擇p<-ggplot(ToothGrowth, aes(x=dose, y=len, color=dose))+ geom_boxplot()p
也可以使用以下功能手動更改箱式圖的線條顏色:
·scale_color_manual():使用自定義顏色
·scale_color_brewer():使用RColorBrewer包中的調色板(詳情可參看第四講插入連結)
·scale_color_grey():使用灰度調色板
p + scale_color_manual(values=c("#999999", "#E69F00", "#56B4E9"))p + scale_color_brewer(palette="Dark2")p+scale_color_grey()+theme_classic()
在下面的R代碼中,箱式圖填充顏色由dose水平自動控制:
ggplot(ToothGrowth, aes(x=dose, y=len))+ geom_boxplot(fill='#A4A4A4', color="black")+ theme_classic()p<-ggplot(ToothGrowth, aes(x=dose, y=len, fill=dose))+ geom_boxplot()p
也可以使用以下功能手動更改箱式圖填充顏色:
·scale_fill_manual():使用自定義顏色
·scale_fill_brewer():使用RColorBrewer包中的調色板
·scale_fill_grey():使用灰度調色板
p+scale_fill_manual(values=c("#999999", "#E69F00", "#56B4E9"))p+scale_fill_brewer(palette="Dark2")p+scale_fill_grey()+theme_classic()
p+theme(legend.position="top")p+theme(legend.position="bottom")p+theme(legend.position="none")
參數legend.position的允許值為:「 left」,「 top」,「 right」,「 bottom」。
函數scale_x_discrete可用於將項目順序更改為「 2」,「 0.5」,「 1」:
p+scale_x_discrete(limits=c("2", "0.5", "1"))
# 更改組別更改顏色:fill=suppggplot(ToothGrowth, aes(x=dose, y=len,fill=supp))+ geom_boxplot()# 更改位置:position=position_dodge(1),組別間有間隔p<-ggplot(ToothGrowth, aes(x=dose, y=len, fill=supp))+ geom_boxplot(position=position_dodge(1))p
更改箱式圖的顏色並添加點:
# 更加組別更改顏色:fill=suppggplot(ToothGrowth, aes(x=dose, y=len,fill=supp))+ geom_boxplot()# 更改位置:position=position_dodge(1),組別間有間隔p<-ggplot(ToothGrowth, aes(x=dose, y=len, fill=supp))+ geom_boxplot(position=position_dodge(1))p
# 基本箱式圖,填充灰色(fill=」grey」),labs用於設立標題和軸標題ggplot(ToothGrowth, aes(x=dose, y=len))+ geom_boxplot(fill="gray")+ labs(title="Plot of length per dose",x="Dose (mg)", y="Length")+ theme_classic()# 根據組別自動改變箱式圖顏色bp<-ggplot(ToothGrowth, aes(x=dose, y=len, fill=dose))+ geom_boxplot()+ labs(title="Plot of length perdose",x="Dose (mg)", y="Length")bp+theme_classic()
手動更改填充顏色:
bp+scale_fill_brewer(palette="Blues")+theme_classic()bp+scale_fill_brewer(palette="Dark2")+theme_minimal()bp+scale_fill_brewer(palette="RdBu")+theme_minimal()
參考內容:Alboukadel Kassambara, GGPlot2 Essentials for Great Data Visualization in R
好了,本期講解就先到這裡。小夥伴們趕緊試起來吧。在下一講中,我們將進行R作圖-ggplot2繪製小提琴圖的講解。歡迎關注,投必得醫學,手把手帶您走入R語言的世界。
快掃二維碼撩客服,
帶你進入投必得醫學交流群,
讓我們共同進步!
↓↓
- END -
長按二維碼關注「投必得醫學」,更多科研乾貨在等你!