R可视化学习(2)--箱线图

简介: 箱线图由箱和“须”(whisker)两部分组成。箱的范围是从数据的下四分位数到上四分位数,也就是常说的四分位距(IQR)。箱的中间有一条表示中位数,或者说50%分位数的线。须则是从箱子的边缘出发延伸至1.5倍四分位距内的最远的点。如果图中有超过须的数据点,则其被视为异常值,并以点来表示。如下图使用偏态的数据展示了直方图、密度曲线和箱线图之间的关系。

箱线图由箱和“须”(whisker)两部分组成。箱的范围是从数据的下四分位数到上四分位数,也就是常说的四分位距(IQR)。箱的中间有一条表示中位数,或者说50%分位数的线。须则是从箱子的边缘出发延伸至1.5倍四分位距内的最远的点。如果图中有超过须的数据点,则其被视为异常值,并以点来表示。如下图使用偏态的数据展示了直方图、密度曲线和箱线图之间的关系。

dfae9134ada8f4e100192a561c81d02.png

本篇介绍如何使用ggplot2制作箱线图,我们使用geom_boxplot()函数,一个简单的格式如下:

geom_boxplot(outlier.colour="black", outlier.shape=16,
             outlier.size=2, notch=FALSE)

outlier.colour, outlier.shape, outlier.size : 展示离群值的颜色,形状,大小

notch : 逻辑值。如果为真,画一个有缺口的方框图。凹口显示中值周围的置信区间,该区间通常基于中值+/- 1.58*IQR/sqrt(n)。槽口用于比较组;如果两个盒子的凹口没有重叠,这是中间值不同的有力证据。

eb44b1cf0f9cdad3394aea32dca07cd.png

准备数据

使用ToothGrowth数据集

# Convert the variable dose from a numeric to a factor variable
ToothGrowth$dose <- as.factor(ToothGrowth$dose)
head(ToothGrowth)

基本的箱线图

library(ggplot2)
# Basic box plot
p <- ggplot(ToothGrowth, aes(x=dose, y=len)) + 
  geom_boxplot()
p
# Rotate the box plot
p + coord_flip()
# Notched box plot
ggplot(ToothGrowth, aes(x=dose, y=len)) + 
  geom_boxplot(notch=TRUE)
# Change outlier, color, shape and size
ggplot(ToothGrowth, aes(x=dose, y=len)) + 
  geom_boxplot(outlier.colour="red", outlier.shape=8,
                outlier.size=4)

95009dbd789a80f48d4f4f9c0d440a8.png

添加平均值

使用stat_summary()函数

# Box plot with mean points
p + stat_summary(fun.y=mean, geom="point", shape=23, size=4)

328c69dabbb1652c66048a29d55600c.png

选择指定组展示

p + scale_x_discrete(limits=c("0.5", "2"))

d678ba82e32c1c3de2149f81e0ae846.png

带有点的箱线图

通过geom_dotplot()函数或者geom_jitter()函数添加点

# Box plot with dot plot
p + geom_dotplot(binaxis='y', stackdir='center', dotsize=1)
# Box plot with jittered points
# 0.2 : degree of jitter in x direction
p + geom_jitter(shape=16, position=position_jitter(0.2))

5de57b8369fb7b74e06acd665f8f707.png

映射分组颜色

改变箱子的线条颜色

盒子图线颜色可以通过变量不同水平自动控制:

# Change box plot line colors by groups
p<-ggplot(ToothGrowth, aes(x=dose, y=len, color=dose)) +
  geom_boxplot()
p

dd7bd4b46781ab9abcc00c6173ce94f.png

我们也可以使用下列的函数手动指定箱子的颜色

  • scale_color_manual() : to use custom colors
  • scale_color_brewer() : to use color palettes from RColorBrewer package
  • scale_color_grey() : to use grey color palettes

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

be883adf073570a90396b88d312907c.png

改变箱子的填充颜色

# Use single color
ggplot(ToothGrowth, aes(x=dose, y=len)) +
  geom_boxplot(fill='#A4A4A4', color="black")+
  theme_classic()
# Change box plot colors by groups
p<-ggplot(ToothGrowth, aes(x=dose, y=len, fill=dose)) +
  geom_boxplot()
p

也可以手动改变填充颜色

  • scale_fill_manual() : to use custom colors
  • scale_fill_brewer() : to use color palettes from RColorBrewer package
  • scale_fill_grey() : to use grey color palettes

# Use custom color palettes
p+scale_fill_manual(values=c("#999999", "#E69F00", "#56B4E9"))
# use brewer color palettes
p+scale_fill_brewer(palette="Dark2")
# Use grey scale
p + scale_fill_grey() + theme_classic()

890907ec13ae8bdabddfc071526ecd6.png

改变图例位置

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

5c1412776e4eec3828014adce4d70db.png

改变x轴变量的顺序

p + scale_x_discrete(limits=c("2", "0.5", "1"))

多组箱线图

# Change box plot colors by groups
ggplot(ToothGrowth, aes(x=dose, y=len, fill=supp)) +
  geom_boxplot()
# Change the position
p<-ggplot(ToothGrowth, aes(x=dose, y=len, fill=supp)) +
  geom_boxplot(position=position_dodge(1))
p

86e3f734cf3f8b29961e99f5bda18dc.png

改变颜色并添加点

# Add dots
p + geom_dotplot(binaxis='y', stackdir='center',
                 position=position_dodge(1))
# Change colors
p+scale_fill_manual(values=c("#999999", "#E69F00", "#56B4E9"))

07ec48080d76f232ff2bf42522867e4.png

自定义箱线图

添加标题或改变主题

# Basic box plot
ggplot(ToothGrowth, aes(x=dose, y=len)) + 
  geom_boxplot(fill="gray")+
  labs(title="Plot of length per dose",x="Dose (mg)", y = "Length")+
  theme_classic()
# Change  automatically color by groups
bp <- ggplot(ToothGrowth, aes(x=dose, y=len, fill=dose)) + 
  geom_boxplot()+
  labs(title="Plot of length  per dose",x="Dose (mg)", y = "Length")
bp + theme_classic()

279cb9f083be9433b5a8fe17405a51d.png

手动改变颜色

# Continuous colors
bp + scale_fill_brewer(palette="Blues") + theme_classic()
# Discrete colors
bp + scale_fill_brewer(palette="Dark2") + theme_minimal()
# Gradient colors
bp + scale_fill_brewer(palette="RdBu") + theme_minimal()

157d765612c2e938d93deb5a0130c2b.png

相关文章
|
18天前
|
数据可视化 开发者 Python
Python学习笔记之Matplotlib模块入门(直线图、折线图、曲线图、散点图、柱状图、饼状图、直方图、等高线图和三维图的绘制)-1
Python学习笔记之Matplotlib模块入门(直线图、折线图、曲线图、散点图、柱状图、饼状图、直方图、等高线图和三维图的绘制)
|
18天前
|
Python
Python学习笔记之Matplotlib模块入门(直线图、折线图、曲线图、散点图、柱状图、饼状图、直方图、等高线图和三维图的绘制)-2
Python学习笔记之Matplotlib模块入门(直线图、折线图、曲线图、散点图、柱状图、饼状图、直方图、等高线图和三维图的绘制)
|
2月前
R语言中绘制箱形图的替代品:蜂群图和小提琴图
R语言中绘制箱形图的替代品:蜂群图和小提琴图
|
9月前
|
数据挖掘
这图怎么画| 批量小提琴图+箱线图+散点+差异分析
这图怎么画| 批量小提琴图+箱线图+散点+差异分析
184 0
|
11月前
|
数据可视化 数据挖掘 Linux
数据可视化丨优雅的绘制带显著性标记的箱线散点图,主要使用ggsignif和ggplot2
数据可视化丨优雅的绘制带显著性标记的箱线散点图,主要使用ggsignif和ggplot2
|
数据可视化
R可视化学习(1)--直方图
本篇介绍如何使用R软件和ggplot2包来制作直方图,我们需要用到geom_histgramh函数,也可以用geom_vline函数去增加线条展示平均值。
65 0
|
数据可视化 Python
Matplotlib数据可视化:饼图与箱线图
Matplotlib数据可视化:饼图与箱线图
Matplotlib数据可视化:饼图与箱线图
|
数据可视化 Linux API
Py之seaborn:数据可视化seaborn库(一)的柱状图、箱线图(置信区间图)、散点图/折线图、核密度图/等高线图、盒形图/小提琴图/LV多框图的简介、使用方法之最强攻略(建议收藏)
Py之seaborn:数据可视化seaborn库(一)的柱状图、箱线图(置信区间图)、散点图/折线图、核密度图/等高线图、盒形图/小提琴图/LV多框图的简介、使用方法之最强攻略(建议收藏)
Py之seaborn:数据可视化seaborn库(一)的柱状图、箱线图(置信区间图)、散点图/折线图、核密度图/等高线图、盒形图/小提琴图/LV多框图的简介、使用方法之最强攻略(建议收藏)
|
Python
python matplotlib绘制 3D图像专题 (三维柱状图、曲面图、散点图、曲线图合集)
python matplotlib绘制 3D图像专题 (三维柱状图、曲面图、散点图、曲线图合集)
1338 0
python matplotlib绘制 3D图像专题 (三维柱状图、曲面图、散点图、曲线图合集)
|
数据可视化 Python
Python--Matplotlib库与数据可视化②--常见图形的绘制(柱状图,直方图,饼图,箱线图)
Python--Matplotlib库与数据可视化②--常见图形的绘制(柱状图,直方图,饼图,箱线图)
423 0
Python--Matplotlib库与数据可视化②--常见图形的绘制(柱状图,直方图,饼图,箱线图)