這個系列我們學(xué)習(xí)繪制箱圖的繪制技巧,其實(shí)這也適用于類似小提琴圖等。
library(ggpubr)
library(RColorBrewer)
data("ToothGrowth")
head(ToothGrowth)

先畫個基本的箱圖:
ToothGrowth$dose <- as.factor(ToothGrowth$dose)
ggplot(ToothGrowth, aes(dose, y=len,fill=dose)) +
geom_boxplot(notch=TRUE) +
xlab("Dose")+
ylab("Len")

ggplot(ToothGrowth, aes(dose, y=len,fill=dose)) +
geom_boxplot(notch=TRUE) +? ?#設(shè)置notch
xlab("Dose")+
ylab("Len")+
coord_flip()? ? ? #旋轉(zhuǎn)箱線圖方向

修改異常點(diǎn)的屬性,設(shè)置outlier的 color, shape and size, outlier.fill:離群點(diǎn)的填充色;outlier.alpha:離群點(diǎn)的透明度
ggplot(ToothGrowth, aes(dose, y=len,fill=dose)) +
geom_boxplot(notch=TRUE,outlier.colour="red", outlier.shape=18,outlier.size=4) +
xlab("Dose")+
ylab("Len")

ggplot(ToothGrowth, aes(dose, y=len,fill=dose)) +
geom_boxplot(notch=TRUE,outlier.colour="red", outlier.shape=18,outlier.size=4) +
xlab("Dose")+
ylab("Len")+
stat_boxplot(geom = "errorbar",width=0.15)? #添加最大值和最小值的兩條須線

ggplot(ToothGrowth, aes(dose, y=len,fill=dose)) +
geom_boxplot(notch=TRUE,outlier.colour="red", outlier.shape=18,outlier.size=2) +
xlab("Dose")+
ylab("Len")+
stat_boxplot(geom = "errorbar",width=0.15)+
#geom_dotplot(binaxis='y', stackdir='center', dotsize=0.1, binwidth = 1) #geom_point函數(shù),向箱線圖中添加點(diǎn)
geom_jitter(shape=16, size=0.01)? #geom_jitter()函數(shù)是geom_point(position = "jitter")的包裝

下面進(jìn)行顏色等的調(diào)整。
ggplot(ToothGrowth, aes(dose, y=len,fill=dose)) +
geom_boxplot(notch=TRUE,outlier.colour="red", outlier.shape=18,outlier.size=2) +
xlab("Dose")+
ylab("Len")+
stat_boxplot(geom = "errorbar",width=0.15)+
#geom_dotplot(binaxis='y', stackdir='center', dotsize=0.1, binwidth = 1)
geom_jitter(shape=16, size=0.01)+? #geom_jitter()函數(shù)是geom_point(position = "jitter")的包裝
scale_fill_manual(values = c("red","blue","green"))+
theme_classic()+
theme(text = element_text(size = 20))

下面,我們添加不同組之間的統(tǒng)計檢驗,主要通過stat_compare_means函數(shù)實(shí)現(xiàn)。
stat_compare_means(mapping = NULL, comparisons = NULL hide.ns = FALSE,
label = NULL, ?label.x = NULL, label.y = NULL, ?...)
mapping:通過aes()設(shè)置繪圖時的aesthetic
comparisons:指定一個列表(list),每個列表元素需為長度等于2的向量。向量的內(nèi)容可以為X軸的兩個組別名(字符型),也可以是兩個感興趣組的組別索引(整數(shù)值),表示采用指定的兩個組別進(jìn)行比較。
hide.ns:邏輯變量,如果設(shè)為TRUE,顯示顯著性水平時將隱藏 ns 字樣,即組間差異不顯著時不顯示 ns 字樣。
label:指定一個字符串,表示標(biāo)簽類型??蔀椋骸皃.signif”(顯示顯著性水平),“p.format”(顯示格式化的P值)。
label.x, label.y:指定一個數(shù)值,表示顯示標(biāo)簽的絕對坐標(biāo)位置。
…:傳遞給函數(shù)compare_means()的參數(shù),如method、paired、ref.group。
my_comparisons <- list( c("0.5", "1"), c("1", "2"), c("0.5", "2") )
ggplot(ToothGrowth, aes(dose, y=len,fill=dose)) +
geom_boxplot(notch=TRUE,outlier.colour="red", outlier.shape=18,outlier.size=2) +
xlab("Dose")+
ylab("Len")+
stat_boxplot(geom = "errorbar",width=0.15)+
#geom_dotplot(binaxis='y', stackdir='center', dotsize=0.1, binwidth = 1)
geom_jitter(shape=16, size=0.01)+? #geom_jitter()函數(shù)是geom_point(position = "jitter")的包裝
scale_fill_manual(values = c("red","blue","green"))+
theme_classic()+
theme(text = element_text(size = 20))+
stat_compare_means(comparisons = my_comparisons)+ ## 增加兩兩比較的p-value
stat_compare_means(label.y = 50,label.x = 1.5)? ? # 增加全局p-value

可以調(diào)整其它統(tǒng)計檢驗方法。
ggplot(ToothGrowth, aes(dose, y=len,fill=dose)) +
geom_boxplot(notch=TRUE,outlier.colour="red", outlier.shape=18,outlier.size=2) +
xlab("Dose")+
ylab("Len")+
stat_boxplot(geom = "errorbar",width=0.15)+
#geom_dotplot(binaxis='y', stackdir='center', dotsize=0.1, binwidth = 1)
geom_jitter(shape=16, size=0.01)+? #geom_jitter()函數(shù)是geom_point(position = "jitter")的包裝
scale_fill_manual(values = c("red","blue","green"))+
theme_classic()+
theme(text = element_text(size = 20))+
stat_compare_means(comparisons = my_comparisons,hide.ns = TRUE,method = "t.test",label = "p.signif")+ ## 增加兩兩比較的p-value
stat_compare_means(method = "anova",label.y = 50,label.x = 1.5)? ? # 增加全局p-value
