溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

如何利用R語言的ggplot2包繪制箱線圖

發(fā)布時間:2021-12-14 10:46:06 來源:億速云 閱讀:1624 作者:小新 欄目:大數(shù)據(jù)

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

一 繪制基本的箱線圖

載入數(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),對len變量做箱線圖

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

如何利用R語言的ggplot2包繪制箱線圖

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

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

如何利用R語言的ggplot2包繪制箱線圖

2)修改異常點的屬性

 設(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語言的ggplot2包繪制箱線圖

此外, outlier.fill:離群點的填充色;outlier.alpha:離群點的透明度

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語言的ggplot2包繪制箱線圖

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

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

如何利用R語言的ggplot2包繪制箱線圖

5)箱線圖添加點

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

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

如何利用R語言的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語言的ggplot2包繪制箱線圖

二 顏色設(shè)置

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

1)分組更改箱線的顏色

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

如何利用R語言的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語言的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語言的ggplot2包繪制箱線圖

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

1)設(shè)置legeng

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

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

如何利用R語言的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語言的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語言的ggplot2包繪制箱線圖

看完了這篇文章,相信你對“如何利用R語言的ggplot2包繪制箱線圖”有了一定的了解,如果想了解更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI