首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

ggplot2|从0开始绘制箱线图

继续“一图胜千言”系列,箱线图通过绘制观测数据的五数总括,即最小值、下四分位数、中位数、上四分位数以及最大值,描述了变量值的分布情况。箱线图能够显示出离群点(outlier),通过箱线图能够很容易识别出数据中的异常值。

本文利用R语言的ggplot2包,从头带您绘制各式各样的箱线图。

一 绘制基本的箱线图

载入数据及函数包

library(ggplot2)
library(RColorBrewer)

dose数值 变成因子变量

ToothGrowth$dose <- as.factor(ToothGrowth$dose) 
head(ToothGrowth) #查看数据集
   len supp dose
1  4.2   VC  0.5
2 11.5   VC  0.5
3  7.3   VC  0.5
4  5.8   VC  0.5
5  6.4   VC  0.5
6 10.0   VC  0.5

1)geom_boxplot绘制基本的箱线图

使用ToothGrowth数据集,dose变量为分类横坐标,对len变量做箱线图

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

旋转箱线图方向并设置notch

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

2)修改异常点的属性

设置outlier的 color, shape and size

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

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

3)选择变量,设定顺序

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")) #选择变量,更改顺序

4)添加最大值和最小值的两条须线

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

5)箱线图添加点

geom_point函数,向箱线图中添加点;

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

geom_jitter()函数是geom_point(position = "jitter")的包装,binaxis="y"是指沿着y轴进行分箱;

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

二 颜色设置

aes(color=)函数为每个箱线图设置一个颜色,划分箱线图之后,可以使用scale_color_*()函数自定义颜色。

1)分组更改箱线的颜色

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

自定义颜色方案

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

2)更改箱子填充颜色

fill 填充色 ; color 箱线的外框颜色

#单组 设置颜色

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

#分组 设置颜色 , 自定义颜色设置方案同上

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

三 图例,标题设置

1)设置legeng

Legend是对箱线图的解释性描述,默认的位置是在画布的右侧中间位置,可以通过theme()函数修改Legend的位置

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

2)labs设置标题及坐标标签

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

3)其他theme详细设置可参考ggplot2-theme(主题)以及ggplot2-图形微调(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()

五 参考资料

http://www.sthda.com/english/wiki/ggplot2-box-plot-quick-start-guide-r-software-and-data-visualization

ggplot2:数据分析与图形艺术

好了,就是这么简单,输出基本图形后,根据自己的喜好进行细节的调整即可。

下一篇
举报
领券