【R畫圖學(xué)習(xí)10.1】箱圖和顯著性檢驗1

這個系列我們學(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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 箱圖geom_boxplot 例子數(shù)據(jù)格式 namevalueA3.248795062A12.71400397B1...
    曲涼不見閱讀 3,771評論 0 7
  • 作者:嚴(yán)濤浙江大學(xué)作物遺傳育種在讀研究生(生物信息學(xué)方向)偽碼農(nóng),R語言愛好者,愛開源 ggplot2學(xué)習(xí)筆記之圖...
    Dylan的迪閱讀 2,865評論 0 6
  • 參考自https://www.cnblogs.com/ljhdo/p/4921588.html 作者:悅光陰 箱線...
    onlyme_862a閱讀 11,813評論 0 13
  • 寫在前面 ggplot2 是一個功能強(qiáng)大且靈活的R包 ,由Hadley Wickham 編寫,其用于生成優(yōu)雅的圖...
    Boer223閱讀 28,568評論 0 67
  • 主要從如何看圖、用圖與作圖三個方面來對箱線圖進(jìn)行理解和總結(jié)。 1、看圖 如圖所示,箱線圖是將一組數(shù)據(jù)按照大小順序排...
    dowaves閱讀 77,757評論 17 109

友情鏈接更多精彩內(nèi)容