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:数据分析与图形艺术
好了,就是这么简单,输出基本图形后,根据自己的喜好进行细节的调整即可。
- RxJava2 实战(1) - 后台执行耗时操作,实时通知 UI 更新
- AutoIt木马又一发:暗藏神秘照片
- css3动画从入门到精通
- ReactJs和React Native的那些事
- Linux学习-文件排序和FASTA文件操作
- Bash漏洞再次演进:缓冲区溢出导致远程任意命令执行
- Pandas,让Python像R一样处理数据,但快
- ViewPager 实现 Galler 效果, 中间大图显示,两边小图展示
- 最小生成树-Prim算法和Kruskal算法
- Bash漏洞批量检测工具与修复方案
- 组合模式
- 用Fiddler在Android上抓包(Http+https)
- Architecture Components ViewModel的控制。
- React Native之AppRegistry模块
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法