|
箱線圖是一種廣泛使用的統(tǒng)計(jì)工具,用于可視化變量的分布。它最初由 Spear 引入,后經(jīng) Tukey 推廣普及。 老鄭一直覺(jué)得目前數(shù)據(jù)分析的箱式圖對(duì)離群值不友好,而且容易誤導(dǎo),理論來(lái)說(shuō)離群值它也應(yīng)該體現(xiàn)在箱式圖。 ![]() 在實(shí)際應(yīng)用中,這些圖形可以方便地通過(guò) R 的基礎(chǔ)繪圖功能(如
?? |
geom_skewboxplot() | geom_boxplot(),繪制偏態(tài)感知箱線圖 |
summarise_skewbox() | |
compute_skew_stats() |
install.packages("ggskewboxplots")# 或從GitHub安裝:# install.packages("remotes")# 如果你還沒(méi)安裝 remotes#remotes::install_github("mcavs/ggskewboxplots")install.packages("ggplot2")library(ggskewboxplots)library(ggplot2)# 使用內(nèi)置數(shù)據(jù)集 mpgggplot(mpg, aes(x = class, y = hwy)) + geom_skewboxplot(method = "walker") + # 使用Walker方法 theme_minimal()

mpg$trans1 <- ifelse(mpg$trans=="manual(m5)","manual", ifelse(mpg$trans=="manual(m6)","manual","automatic"))ggplot(mpg, aes(x = class, y = hwy, fill = as.factor(trans1))) + geom_skewboxplot(method = "adil") + # 使用Adil方法 labs(fill = "Transmission") + theme_minimal()+ theme(legend.position = "top")

k(觸須系數(shù))ggplot(mpg, aes(x = class, y = hwy)) + geom_skewboxplot(method = "babura", k = 2) + # 放寬觸須范圍 theme_bw()

library(dplyr)mpg %>%group_by(class) %>%summarise_skewbox(hwy, method = "hubert")# 輸出為tibble,包含調(diào)整后的上下觸須、中位數(shù)、異常值等

若數(shù)據(jù)明顯偏態(tài),建議使用 Babura、Walker 或 Junsawang 方法。
若數(shù)據(jù)近似對(duì)稱(chēng),傳統(tǒng)Tukey方法仍可使用。
可通過(guò) summarise_skewbox() 快速比較不同方法的統(tǒng)計(jì)結(jié)果。
如果需要進(jìn)一步調(diào)整圖形(如顏色、主題、標(biāo)簽等),可沿用 ggplot2 的標(biāo)準(zhǔn)語(yǔ)法。
|
|