參考自https://www.cnblogs.com/ljhdo/p/4921588.html
作者:悅光陰
箱線圖通過繪制觀測(cè)數(shù)據(jù)的五數(shù)總括,即最小值、下四分位數(shù)、中位數(shù)、上四分位數(shù)以及最大值,描述了變量值的分布情況。箱線圖能夠顯示出離群點(diǎn)(outlier),離群點(diǎn)也叫做異常值,通過箱線圖能夠很容易識(shí)別出數(shù)據(jù)中的異常值。
箱線圖的各個(gè)組成部分的名稱及其位置如下圖所示:
箱線圖可以直觀地看出數(shù)據(jù)集的以下重要性值:
中心位置:中位數(shù)所在的位置就是數(shù)據(jù)集的中心;
散布程度:箱線圖分為多個(gè)區(qū)間,區(qū)間較短時(shí),表示落在該區(qū)間的點(diǎn)較集中;
對(duì)稱性:如果中位數(shù)位于箱子的中間位置,那么數(shù)據(jù)分布較為對(duì)稱;如果極值離中位數(shù)的距離較大,那么表示數(shù)據(jù)分布傾斜。
一,繪制箱線圖
通常情況下,我們使用ggplot和geom_boxplot繪制箱線圖,本次使用R內(nèi)置數(shù)據(jù)ToothGrowth作為示例數(shù)據(jù):
library(ggplot2)
ToothGrowth$dose <- as.factor(ToothGrowth$dose)
ggplot(ToothGrowth, aes(x=dose, y=len)) + geom_boxplot()
最基本的箱線圖如下:
2,設(shè)置離群點(diǎn)(outlier)
geom_boxplot函數(shù)中有outlier開頭的多個(gè)參數(shù),用于修改離群點(diǎn)的屬性:
outlier.colour:離群點(diǎn)的顏色
outlier.fill:離群點(diǎn)的填充色
outlier.shape:離群點(diǎn)的形狀
outlier.size:離群點(diǎn)的大小
outlier.alpha:離群點(diǎn)的透明度
ggplot(ToothGrowth, aes(x=dose, y=len)) +
? geom_boxplot(outlier.colour="blue", outlier.shape=25, outlier.size=10,outlier.alpha=1,outlier.fill="red")
3,設(shè)置箱線圖的顏色
通過aes(color=)函數(shù)可以為每個(gè)箱線圖設(shè)置一個(gè)顏色,而箱線圖的劃分是通過aes(color=)函數(shù)的color參數(shù)來劃分的,劃分箱線圖之后,scale_color_*()函數(shù)才會(huì)起作用,該函數(shù)用于為每個(gè)箱線圖設(shè)置前景色和填充色,顏色是自定義的:
scale_fill_manual() #for box plot, bar plot, violin plot, etc
scale_color_manual() #for lines and points
以下代碼設(shè)置箱線圖的前景色:
ggplot(ToothGrowth, aes(x=dose, y=len,color=dose)) +
? geom_boxplot()+? scale_color_manual(values=c("#999999","#E69F00","#56B4E9"))
4,設(shè)置Legend 的位置
說明(Legend)是對(duì)箱線圖的解釋性描述,默認(rèn)的位置是在畫布的右側(cè)中間位置,可以通過theme()函數(shù)修改Legend的位置,lengend.position的有效值是top、right、left、bottom和none,默認(rèn)值是right:
p <- ggplot(ToothGrowth, aes(x=dose, y=len,color=dose)) +
? geom_boxplot()+
? scale_color_manual(values=c("#999999", "#E69F00", "#56B4E9"))
p + theme(legend.position="top")
p + theme(legend.position="bottom")
p + theme(legend.position="none") # Remove legend
5,設(shè)置箱線圖的標(biāo)題和坐標(biāo)軸的名稱
通過labs設(shè)置箱線圖的標(biāo)題和坐標(biāo)的名稱,參數(shù)title用于設(shè)置標(biāo)題,x和y用于設(shè)置x軸和y軸的標(biāo)簽:
ggplot(ToothGrowth, aes(x=dose, y=len,color=dose)) +
? geom_boxplot()+? scale_color_manual(values=c("#999999","#E69F00","#56B4E9"))+? theme(legend.position="right")+? labs(title="Plot of length? per dose",x="Dose (mg)", y ="Length")
6,繪制箱線圖的散點(diǎn)
通過geom_point函數(shù),向箱線圖中添加點(diǎn),geom_jitter()函數(shù)是geom_point(position = "jitter")的包裝,binaxis="y"是指沿著y軸進(jìn)行分箱:
# Box plot with dot plot
p + geom_dotplot(binaxis='y', stackdir='center', dotsize=1)
# Box plot with jittered points
# 0.2: degree of jitterin x direction
p + geom_jitter(shape=16, position=position_jitter(0.2))
7,旋轉(zhuǎn)箱線圖
函數(shù)coord_flip()用于翻轉(zhuǎn)笛卡爾坐標(biāo)系,使水平變?yōu)榇怪保怪弊優(yōu)樗剑饕糜诎扬@示y條件x的geoms和統(tǒng)計(jì)信息轉(zhuǎn)換為x條件y。
p <- ggplot(ToothGrowth, aes(x=dose, y=len)) +
? geom_boxplot() +
? coord_flip()
二,異常值檢測(cè)
繪制散點(diǎn)圖,并標(biāo)記異常值:
ggplot(ToothGrowth, aes(x=dose, y=len,color=dose)) +
? geom_boxplot(outlier.colour="red", outlier.shape=7,outlier.size=4)+
? scale_color_manual(values=c("#999999", "#E69F00", "#56B4E9"))+
? theme(legend.position="right")+
? labs(title="Plot of length? per dose",x="Dose (mg)", y = "Length")+
? geom_dotplot(binaxis='y', stackdir='center', stackratio=1.5, dotsize=1.2)
當(dāng)箱線圖中的異常值過多時(shí),繪制的圖中,箱子被壓成一條線,無法觀察到數(shù)據(jù)的分布,這就需要移除異常值,只保留適量的離群點(diǎn),常見的做法是改變ylim的范圍,代碼是:
# compute lower and upper whiskers
ylim1 = boxplot.stats(df$y)$stats[c(1,5)]
# scale y limits based on ylim1
ggplot() + gemo_box() + coord_cartesian(ylim = ylim1*1.05)