ggplot2|从0开始绘制箱线图

简介: ggplot2|从0开始绘制箱线图

本文首发于“生信补给站”公众号 ggplot2|从0开始绘制箱线图

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

image.png

本文利用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()

image.png

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()

image.png

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

image.png

自定义颜色方案


# 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()

image.png

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()

image.png

三 图例,标题设置


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")

image.png

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()

image.png


五 参考资料

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

ggplot2:数据分析与图形艺术


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

相关文章
|
3月前
|
数据可视化 Python
使用pygal库绘制直方图、XY线图和饼状图的技术指南
使用pygal库绘制直方图、XY线图和饼状图的技术指南
20 0
|
8月前
|
数据可视化 数据挖掘 Linux
数据可视化丨优雅的绘制带显著性标记的箱线散点图,主要使用ggsignif和ggplot2
数据可视化丨优雅的绘制带显著性标记的箱线散点图,主要使用ggsignif和ggplot2
|
机器学习/深度学习 API Python
seaborn画直方图、条形图、盒图、散点图等常用图形
seaborn画直方图、条形图、盒图、散点图等常用图形
202 0
seaborn画直方图、条形图、盒图、散点图等常用图形
|
10月前
|
数据挖掘
ggplot2|从0开始绘制折线图
ggplot2|从0开始绘制折线图
117 0
|
10月前
|
数据挖掘
ggplot2|从0开始绘制直方图
ggplot2|从0开始绘制直方图
160 0
|
11月前
|
数据可视化 数据挖掘 Python
R语言ggplot2作图小技巧:柱形图如何让y轴的起始点不是0
R语言ggplot2作图小技巧:柱形图如何让y轴的起始点不是0
|
11月前
|
数据可视化 数据挖掘 Python
跟着GlobalChangeBiology学作图:R语言ggplot2点线图(2)给分面添加注释
跟着GlobalChangeBiology学作图:R语言ggplot2点线图(2)给分面添加注释
matplotlib 绘制一个灰度直方图
matplotlib 绘制一个灰度直方图
matplotlib 绘制一个灰度直方图
|
开发者 Python
matplotlib画折线图、直方图、饼图、散点图等常见图形
matplotlib画折线图、直方图、饼图、散点图等常见图形
206 0
matplotlib画折线图、直方图、饼图、散点图等常见图形
|
定位技术
pyecharts绘制条形图、饼图、散点图、词云图、地图等常用图形(二)
pyecharts绘制条形图、饼图、散点图、词云图、地图等常用图形
133 0
pyecharts绘制条形图、饼图、散点图、词云图、地图等常用图形(二)