參考自https://www.cnblogs.com/ljhdo/p/4921588.html
作者:悅光陰
箱線圖通過繪制觀測數(shù)據(jù)的五數(shù)總括栅表,即最小值笋鄙、下四分位數(shù)、中位數(shù)怪瓶、上四分位數(shù)以及最大值,描述了變量值的分布情況践美。箱線圖能夠顯示出離群點(outlier)洗贰,離群點也叫做異常值,通過箱線圖能夠很容易識別出數(shù)據(jù)中的異常值陨倡。
箱線圖的各個組成部分的名稱及其位置如下圖所示:
箱線圖可以直觀地看出數(shù)據(jù)集的以下重要性值:
中心位置:中位數(shù)所在的位置就是數(shù)據(jù)集的中心敛滋;
散布程度:箱線圖分為多個區(qū)間,區(qū)間較短時兴革,表示落在該區(qū)間的點較集中绎晃;
對稱性:如果中位數(shù)位于箱子的中間位置,那么數(shù)據(jù)分布較為對稱杂曲;如果極值離中位數(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棚饵,設置離群點(outlier)
geom_boxplot函數(shù)中有outlier開頭的多個參數(shù)煤裙,用于修改離群點的屬性:
outlier.colour:離群點的顏色
outlier.fill:離群點的填充色
outlier.shape:離群點的形狀
outlier.size:離群點的大小
outlier.alpha:離群點的透明度
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,設置箱線圖的顏色
通過aes(color=)函數(shù)可以為每個箱線圖設置一個顏色噪漾,而箱線圖的劃分是通過aes(color=)函數(shù)的color參數(shù)來劃分的硼砰,劃分箱線圖之后,scale_color_*()函數(shù)才會起作用欣硼,該函數(shù)用于為每個箱線圖設置前景色和填充色题翰,顏色是自定義的:
scale_fill_manual() #for box plot, bar plot, violin plot, etc
scale_color_manual() #for lines and points
以下代碼設置箱線圖的前景色:
ggplot(ToothGrowth, aes(x=dose, y=len,color=dose)) +
? geom_boxplot()+? scale_color_manual(values=c("#999999","#E69F00","#56B4E9"))
4,設置Legend 的位置
說明(Legend)是對箱線圖的解釋性描述分别,默認的位置是在畫布的右側中間位置遍愿,可以通過theme()函數(shù)修改Legend的位置,lengend.position的有效值是top耘斩、right沼填、left、bottom和none括授,默認值是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坞笙,設置箱線圖的標題和坐標軸的名稱
通過labs設置箱線圖的標題和坐標的名稱岩饼,參數(shù)title用于設置標題,x和y用于設置x軸和y軸的標簽:
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薛夜,繪制箱線圖的散點
通過geom_point函數(shù)籍茧,向箱線圖中添加點,geom_jitter()函數(shù)是geom_point(position = "jitter")的包裝梯澜,binaxis="y"是指沿著y軸進行分箱:
# 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寞冯,旋轉箱線圖
函數(shù)coord_flip()用于翻轉笛卡爾坐標系,使水平變?yōu)榇怪蓖砘铮怪弊優(yōu)樗剿绷洌饕糜诎扬@示y條件x的geoms和統(tǒng)計信息轉換為x條件y。
p <- ggplot(ToothGrowth, aes(x=dose, y=len)) +
? geom_boxplot() +
? coord_flip()
二咆疗,異常值檢測
繪制散點圖漓帚,并標記異常值:
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)
當箱線圖中的異常值過多時,繪制的圖中午磁,箱子被壓成一條線尝抖,無法觀察到數(shù)據(jù)的分布,這就需要移除異常值迅皇,只保留適量的離群點昧辽,常見的做法是改變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)