如何利用R語(yǔ)言的ggplot2包繪制箱線圖

小編給大家分享一下如何利用R語(yǔ)言的ggplot2包繪制箱線圖,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!

創(chuàng)新互聯(lián)建站基于分布式IDC數(shù)據(jù)中心構(gòu)建的平臺(tái)為眾多戶提供樂山服務(wù)器托管 四川大帶寬租用 成都機(jī)柜租用 成都服務(wù)器租用。

一 繪制基本的箱線圖

載入數(shù)據(jù)及函數(shù)包

library(ggplot2)library(RColorBrewer)

dose數(shù)值 變成因子變量

ToothGrowth$dose <- as.factor(ToothGrowth$dose) head(ToothGrowth) #查看數(shù)據(jù)集   len supp dose1  4.2   VC  0.52 11.5   VC  0.53  7.3   VC  0.54  5.8   VC  0.55  6.4   VC  0.56 10.0   VC  0.5

1)geom_boxplot繪制基本的箱線圖

使用ToothGrowth數(shù)據(jù)集,dose變量為分類橫坐標(biāo),對(duì)len變量做箱線圖

ggplot(ToothGrowth, aes(x=dose, y=len)) + geom_boxplot()

如何利用R語(yǔ)言的ggplot2包繪制箱線圖

旋轉(zhuǎn)箱線圖方向并設(shè)置notch

ggplot(ToothGrowth, aes(x=dose, y=len)) + geom_boxplot(notch=TRUE) + coord_flip()

如何利用R語(yǔ)言的ggplot2包繪制箱線圖

2)修改異常點(diǎn)的屬性

 設(shè)置outlier的 color, shape and size 

ggplot(ToothGrowth, aes(x=dose, y=len)) + geom_boxplot(outlier.colour="red", outlier.shape=18,outlier.size=4)

如何利用R語(yǔ)言的ggplot2包繪制箱線圖

此外, outlier.fill:離群點(diǎn)的填充色;outlier.alpha:離群點(diǎn)的透明度

3)選擇變量,設(shè)定順序

ggplot(ToothGrowth, aes(x=dose, y=len)) + geom_boxplot() + stat_summary(fun.y=mean, geom="point", shape=23, size=4, col = "red") +  #添加均值scale_x_discrete(limits=c("2", "0.5")) #選擇變量,更改順序

如何利用R語(yǔ)言的ggplot2包繪制箱線圖

4)添加最大值和最小值的兩條須線

ggplot(ToothGrowth, aes(x=dose, y=len)) + stat_boxplot(geom = "errorbar",width=0.15) + #添加虛線geom_boxplot()

如何利用R語(yǔ)言的ggplot2包繪制箱線圖

5)箱線圖添加點(diǎn)

geom_point函數(shù),向箱線圖中添加點(diǎn);

ggplot(ToothGrowth, aes(x=dose, y=len)) + geom_boxplot() + geom_dotplot(binaxis='y', stackdir='center', dotsize=1, binwidth = 1)

如何利用R語(yǔ)言的ggplot2包繪制箱線圖

geom_jitter()函數(shù)是geom_point(position = "jitter")的包裝,binaxis="y"是指沿著y軸進(jìn)行分箱;

ggplot(ToothGrowth, aes(x=dose, y=len)) + geom_boxplot() + geom_jitter(shape=16, position=position_jitter(0.2))

如何利用R語(yǔ)言的ggplot2包繪制箱線圖

二 顏色設(shè)置

aes(color=)函數(shù)為每個(gè)箱線圖設(shè)置一個(gè)顏色,劃分箱線圖之后,可以使用scale_color_*()函數(shù)自定義顏色。

1)分組更改箱線的顏色

p<-ggplot(ToothGrowth, aes(x=dose, y=len, color=dose)) + geom_boxplot()p

如何利用R語(yǔ)言的ggplot2包繪制箱線圖

自定義顏色方案

# Use custom color palettesp+scale_color_manual(values=c("#999999", "#E69F00", "skyblue"))# Use brewer color palettesp+scale_color_brewer(palette="Set3")+ theme_classic()# Use grey scalep + scale_color_grey() + theme_classic()

如何利用R語(yǔ)言的ggplot2包繪制箱線圖

2)更改箱子填充顏色

fill 填充色 ; color 箱線的外框顏色

#單組 設(shè)置顏色

ggplot(ToothGrowth, aes(x=dose, y=len)) + geom_boxplot(fill='#A4A4A4', color="black")+ theme_classic()

#分組 設(shè)置顏色 , 自定義顏色設(shè)置方案同上

ggplot(ToothGrowth, aes(x=dose, y=len, fill=dose)) + geom_boxplot() + scale_fill_brewer(palette="Dark2") + theme_classic()

如何利用R語(yǔ)言的ggplot2包繪制箱線圖

三 圖例,標(biāo)題設(shè)置

1)設(shè)置legeng

Legend是對(duì)箱線圖的解釋性描述,默認(rèn)的位置是在畫布的右側(cè)中間位置,可以通過theme()函數(shù)修改Legend的位置

p + theme(legend.position="top")p + theme(legend.position="bottom")p + theme(legend.position="none") # Remove legend

如何利用R語(yǔ)言的ggplot2包繪制箱線圖

2)labs設(shè)置標(biāo)題及坐標(biāo)標(biāo)簽

p+theme(legend.position="bottom") + labs(title="Plot of length  per dose",x="Dose (mg)", y = "Length")

如何利用R語(yǔ)言的ggplot2包繪制箱線圖

3)其他theme詳細(xì)設(shè)置可參考ggplot2-theme(主題)以及ggplot2-圖形微調(diào)(1)

四 箱線圖匯總展示

ggplot(ToothGrowth, aes(x=dose, y=len, fill=dose)) +   stat_boxplot(geom = "errorbar",width=0.15)+  geom_boxplot()+  geom_dotplot(binaxis='y', stackdir='center', dotsize=0.5, binwidth = 1)+  labs(title="Plot of length  per dose",x="Dose (mg)", y = "Length")+  scale_fill_manual(values=c("#999999", "#E69F00", "#56B4E9")) +   theme(legend.position="none")+  theme_minimal()

如何利用R語(yǔ)言的ggplot2包繪制箱線圖

看完了這篇文章,相信你對(duì)“如何利用R語(yǔ)言的ggplot2包繪制箱線圖”有了一定的了解,如果想了解更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!

文章標(biāo)題:如何利用R語(yǔ)言的ggplot2包繪制箱線圖
URL網(wǎng)址:http://bm7419.com/article0/pcejoo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供云服務(wù)器、微信公眾號(hào)、網(wǎng)站內(nèi)鏈、企業(yè)建站、網(wǎng)站制作、網(wǎng)站改版

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站托管運(yùn)營(yíng)