ggplot2-绘图-箱线图

一、前面的啰嗦

本教程对绘图所需数据和绘图过程进行了一步步的解释,稍显啰嗦,没时间细看或不想一步步看的朋友可以直接点击目录最后一节:
五、箱线图完整版代码

二、绘图数据说明

用到的数据是mpg数据集,这是ggplot2包自带的数据集,可以使用以下代码查看该数据集的基本情况:

library(ggplot2)
?mpg # 或者help(mpg)

从帮助文档中可以看出这是一个234x11的数据框,它记录了美国1999年和2008年部分汽车的制造厂商,型号,类别,驱动程序和耗油量等信息。
下面看一下这11列的情况:

library(ggplot2)
# str查看数据集的基本情况
str(mpg)
## tibble [234 x 11] (S3: tbl_df/tbl/data.frame)
##  $ manufacturer: chr [1:234] "audi" "audi" "audi" "audi" ...
##  $ model       : chr [1:234] "a4" "a4" "a4" "a4" ...
##  $ displ       : num [1:234] 1.8 1.8 2 2 2.8 2.8 3.1 1.8 1.8 2 ...
##  $ year        : int [1:234] 1999 1999 2008 2008 1999 1999 2008 1999 1999 2008 ...
##  $ cyl         : int [1:234] 4 4 4 4 6 6 6 4 4 4 ...
##  $ trans       : chr [1:234] "auto(l5)" "manual(m5)" "manual(m6)" "auto(av)" ...
##  $ drv         : chr [1:234] "f" "f" "f" "f" ...
##  $ cty         : int [1:234] 18 21 20 21 16 18 18 18 16 20 ...
##  $ hwy         : int [1:234] 29 29 31 30 26 26 27 26 25 28 ...
##  $ fl          : chr [1:234] "p" "p" "p" "p" ...
##  $ class       : chr [1:234] "compact" "compact" "compact" "compact" ...

每一列的解释如下:

  • model: model name
  • displ: 发动机排量(engine displacement, in litres)
  • year: 发行时间,仅有1999和2008
  • cyl: 汽缸数目(number of cylinders)
  • trans: 变速器类型(type of transmission)
  • drv: 表示驱动程序,是一个分类变量,详细解释为:the type of drive
    train, where f = front-wheel drive, r = rear wheel drive, 4 = 4wd
  • cty: city miles per gallon,每加仑城市里程,耗油量的一种计量方式。
  • hwy: highway miles per
    gallon,每加仑公路行驶里程,也是耗油量的一种计量方式。

三、基本箱线图

下面对hwy绘制箱线图,查看这些汽车的耗油量分布情况。

library(ggplot2)
ggplot(data=mpg, aes(x="", y=hwy)) +
  geom_boxplot()

unnamed-chunk-3-1.png

可以看到耗油量的中位数在25附近,其中有两个离群点,这两辆汽车的耗油量很大。

这里绘制的图是一个横向的箱线图,如果要绘制纵向的箱线图(即上图顺时针旋转90度),只需要改一下代码中aes的参数,设置为x=hwy即可,如下所示:

ggplot(data=mpg, aes(x=hwy, y="")) +
  geom_boxplot()

unnamed-chunk-4-1.png

四、分组箱线图

1. 单分类变量的分组箱线图

1.1 基本版

下面绘制一下分组箱线图,看一下不同驱动程序的汽车的耗油量分布,其中驱动程序是mpg数据集中的drv变量,这是一个分类变量,首先查看数据情况:

class(mpg$drv)
## [1] "character"
table(mpg$drv)
## 
##   4   f   r 
## 103 106  25

开始画箱线图:

ggplot(data=mpg, aes(x=factor(drv), y=hwy, fill=factor(drv))) +
  geom_boxplot()

unnamed-chunk-6-1.png

由于mpg$drv的类别是character,而ggplot绘图进行分组所需要的变量类型是factor,因此需要将drv变量转为factor类别,但这里存在一个问题,就是横坐标轴标题出现了factor字样,图例的标题中也出现了factor字样,不是很美观,这个问题的解决方法有两个:

a. 直接对数据框的drv变量进行修改,将其改为factor类别:

mpg_new <- mpg
mpg_new$drv <- as.factor(mpg_new$drv)
ggplot(data=mpg_new, aes(x=drv, y=hwy, fill=drv)) +
  geom_boxplot()

unnamed-chunk-7-1.png

b. 修改坐标轴与图例的标题,将其修改为drv:

ggplot(data=mpg, aes(x=factor(drv), y=hwy, fill=factor(drv))) +
  geom_boxplot() + 
  scale_fill_discrete(name='drv') + 
  labs(x="drv") # x表示横坐标轴的标题

unnamed-chunk-8-1.png

  # scale_一系列的函数都是用来配置图例的,scale_fill_discrete指的是修改fill的图例,和aes中的fill相对应。

1.2 美化版

上面的箱线图存在一些地方不是很好看,主要有以下几点:

a. 图例的颜色与横坐标轴的三个标签信息是一致的,存在冗余,因此可以去掉图例,去掉图例的代码为guides(fill=FALSE)
b. 没有添加标题,可以在labs函数中添加标题
c. 所用主题可以优化,可以使用theme_系列函数

下面使用mpg_new数据框进行操作,避免不停的对drv列进行操作。

mpg_new <- mpg
mpg_new$drv <- as.factor(mpg_new$drv)
ggplot(data=mpg_new, aes(x=drv, y=hwy, fill=drv)) +
  geom_boxplot() +
  guides(fill=FALSE) + # fill指的是图例中的fill,和aes中对应
  labs(title="Boxplot of highway miles per gallon") +
  theme_minimal() +
  theme(plot.title=element_text(hjust=0.5)) # 这一句让标题居中,ggplot2默认的标题是左对齐的
## Warning: `guides(<scale> = FALSE)` is deprecated. Please use `guides(<scale> =
## "none")` instead.

unnamed-chunk-9-1.png

这里有一个warning,表示guides(<scale> = FALSE)这种用法已经废弃,这种用法是旧版本ggplot2的用法,新版本的应使用guides(<scale> = "none")(我的ggplot2版本是3.3.5),修改后的代码如下:

ggplot(data=mpg_new, aes(x=drv, y=hwy, fill=drv)) +
  geom_boxplot() +
  guides(fill="none") + # fill指的是图例中的fill,和aes中对应
  labs(title="Boxplot of highway miles per gallon") +
  theme_minimal() +
  theme(plot.title=element_text(hjust=0.5))  #这一句让标题居中,ggplot2默认的标题是左对齐的

unnamed-chunk-10-1.png

这里有一点需要注意的是theme_minimaltheme两个函数的顺序,由于theme_minimal是一种简化式的主题设置,ggplot2遵循图层叠加的原理,如果将theme_minimal放在theme后面,那么图标标题仍然是左对齐,而不是居中显示。

如果不想用fill来表示不同的drv类型,则可以不设置fill=drv,效果如下:

ggplot(data=mpg_new, aes(x=drv, y=hwy)) +
  geom_boxplot() +
  labs(title="Boxplot of highway miles per gallon") +
  theme_minimal() +
  theme(plot.title=element_text(hjust=0.5))  #这一句让标题居中,ggplot2默认的标题是左对齐的

unnamed-chunk-11-1.png

但这样又略显单调,一个更好的解决办法是修改箱线图边框的颜色(color属性),可以设置color=drv,如下所示:

ggplot(data=mpg_new, aes(x=drv, y=hwy, color=drv)) +
  geom_boxplot() +
  guides(color="none") + # 不在图例中显示color的含义
  labs(title="Boxplot of highway miles per gallon") +
  theme_minimal() +
  theme(plot.title=element_text(hjust=0.5))  #这一句让标题居中,ggplot2默认的标题是左对齐的

unnamed-chunk-12-1.png

这样比较美观了,需要注意的是guides中是color=“none”,而不是fill=“none”,因为我们在aes中设置了color=drv,那么color会出现在图例中,设置了color="none"则可以不让图例显示color。

2. 双分类变量的分组箱线图

2.1 基本版

上面只是对不同驱动类型的汽车耗油量绘制了箱线图,但如果我们想再多加一个分类变量呢?每种汽车都有一个year属性,表示制造年份:

library(ggplot2)
table(mpg$year)
## 
## 1999 2008 
##  117  117

可以看到制造年份有1999和2008,那如果我想查看不同制造年份的汽车的三种驱动类型的耗油量情况呢?这就需要对双分类变量进行画图,只需要将其中一个变量映射到fill(或者color)上面,另一个变量仍然作为横坐标即可,如下所示:

# 将year设置为factor类型
mpg_new <- mpg
mpg_new$year <- as.factor(mpg_new$year)
ggplot(mpg_new, aes(x=drv, y=hwy, fill=year)) +
  geom_boxplot()

unnamed-chunk-14-1.png

2.2 美化版

a. 用fill表示不同的年份

# 将year设置为factor类型
mpg_new <- mpg
mpg_new$year <- as.factor(mpg_new$year)
ggplot(mpg_new, aes(x=drv, y=hwy, fill=year)) +
  geom_boxplot() +
  labs(title="Boxplot of highway miles per gallon") + # title是图表标题
  theme_minimal() +
  theme(plot.title=element_text(hjust=0.5))  #这一句让图表标题居中,ggplot2默认的标题是左对齐的

unnamed-chunk-15-1.png

b. 用color表示不同的年份

# 将year设置为factor类型
mpg_new <- mpg
mpg_new$year <- as.factor(mpg_new$year)
ggplot(mpg_new, aes(x=drv, y=hwy, color=year)) +
  geom_boxplot() +
  labs(title="Boxplot of highway miles per gallon") + # title是图表标题
  theme_minimal() +
  theme(plot.title=element_text(hjust=0.5))  #这一句让图表标题居中,ggplot2默认的标题是左对齐的

unnamed-chunk-16-1.png

你觉得哪个更好看呢?

五、箱线图完整版代码

1. 基本箱线图的完整代码

a. 横向箱线图

library(ggplot2)
ggplot(data=mpg, aes(x="", y=hwy)) +
  geom_boxplot() +
  labs(x="") # 去除横坐标轴的标签

unnamed-chunk-17-1.png

b. 纵向箱线图

library(ggplot2)
ggplot(data=mpg, aes(x=hwy, y="")) +
  geom_boxplot() +
  labs(y="") # 去除纵坐标轴的标签

unnamed-chunk-18-1.png

2. 单变量分组箱线图的完整代码

查看不同驱动类型(drv)的汽车耗油量分布情况,可以使用以下代码:

a. 用fill来表示不同的drv

# 首先将数据框中的drv修改为factor类型
library(ggplot2)
mpg_new <- mpg
mpg_new$drv <- as.factor(mpg_new$drv)

# 其次绘图
ggplot(data=mpg_new, aes(x=drv, y=hwy, fill=drv)) +
  geom_boxplot() +
  guides(fill="none") + # fill指的是图例中的fill,和aes中对应,fill=none表示不显示图例
  labs(title="Boxplot of highway miles per gallon") + # title是图表标题
  theme_minimal() +
  theme(plot.title=element_text(hjust=0.5))  #这一句让图表标题居中,ggplot2默认的标题是左对齐的

unnamed-chunk-19-1.png

b. 用color来表示不同的drv

# 首先将数据框中的drv修改为factor类型
library(ggplot2)
mpg_new <- mpg
mpg_new$drv <- as.factor(mpg_new$drv)

# 其次绘图
ggplot(data=mpg_new, aes(x=drv, y=hwy, color=drv)) +
  geom_boxplot() +
  guides(color="none") + # fill指的是图例中的fill,和aes中对应,fill=none表示不显示图例
  labs(title="Boxplot of highway miles per gallon") + # title是图表标题
  theme_minimal() +
  theme(plot.title=element_text(hjust=0.5))  #这一句让图表标题居中,ggplot2默认的标题是左对齐的

unnamed-chunk-20-1.png

3. 双变量分组箱线图的完整代码

查看不同年份制造(year)、不同驱动类型(drv)的汽车的耗油量分布,可以使用以下代码:

a. 用fill来表示不同的年份,横坐标表示drv

library(ggplot2)

# 将year设置为factor类型
mpg_new <- mpg
mpg_new$year <- as.factor(mpg_new$year)
ggplot(mpg_new, aes(x=drv, y=hwy, fill=year)) +
  geom_boxplot() +
  labs(title="Boxplot of highway miles per gallon") + # title是图表标题
  theme_minimal() +
  theme(plot.title=element_text(hjust=0.5))  #这一句让图表标题居中,ggplot2默认的标题是左对齐的

unnamed-chunk-21-1.png

b. 用color来表示不同的年份,横坐标表示drv

library(ggplot2)

# 将year设置为factor类型
mpg_new <- mpg
mpg_new$year <- as.factor(mpg_new$year)
ggplot(mpg_new, aes(x=drv, y=hwy, color=year)) +
  geom_boxplot() +
  labs(title="Boxplot of highway miles per gallon") + # title是图表标题
  theme_minimal() +
  theme(plot.title=element_text(hjust=0.5))  #这一句让图表标题居中,ggplot2默认的标题是左对齐的

unnamed-chunk-22-1.png

c. 用fill表示drv,横坐标表示年份(year)

library(ggplot2)

# 将year设置为factor类型
mpg_new <- mpg
mpg_new$year <- as.factor(mpg_new$year)
ggplot(mpg_new, aes(x=year, y=hwy, fill=drv)) +
  geom_boxplot() +
  labs(title="Boxplot of highway miles per gallon") + # title是图表标题
  theme_minimal() +
  theme(plot.title=element_text(hjust=0.5))  #这一句让图表标题居中,ggplot2默认的标题是左对齐的

unnamed-chunk-23-1.png

d. 用color表示drv,横坐标表示年份(year)

library(ggplot2)

# 将year设置为factor类型
mpg_new <- mpg
mpg_new$year <- as.factor(mpg_new$year)
ggplot(mpg_new, aes(x=year, y=hwy, color=drv)) +
  geom_boxplot() +
  labs(title="Boxplot of highway miles per gallon") + # title是图表标题
  theme_minimal() +
  theme(plot.title=element_text(hjust=0.5))  #这一句让图表标题居中,ggplot2默认的标题是左对齐的

unnamed-chunk-24-1.png

六、思考与优化

上面画了两个分类变量的箱线图,那如果我有3个分类变量呢?

如果有3个分类变量,一种解决方式是将其中一个映射为x轴,第二个映射为fill,第三个映射为color,这样可以画出来,但可能效果不好,不容易一眼看出不同组别之间的差距。

另一种解决方式是,可以按照第三个分类变量进行分面,这样每一面中就是双分类变量的箱线图,下面是一个例子。

比如我想查看不同drv的不同fl(fuel
type)的汽车的耗油量,这里共2个分类变量,那么代码如下:

library(ggplot2)

# 将year设置为factor类型
mpg_new <- mpg
mpg_new$year <- as.factor(mpg_new$year)

ggplot(mpg_new, aes(x=drv, y=hwy, fill=fl)) +
  geom_boxplot()

unnamed-chunk-25-1.png

如果我想将年份加上呢?那就变成了3个分类变量,为了方便起见,由于年份只有2种,所以用年份来进行分面,如下:

# 将year设置为factor类型
mpg_new <- mpg
mpg_new$year <- as.factor(mpg_new$year)

ggplot(mpg_new, aes(x=drv, y=hwy, fill=fl)) +
  geom_boxplot() +
  facet_grid(year ~ .) # 按行分面,不同的年放在不同的行

unnamed-chunk-26-1.png

  • 21
    点赞
  • 124
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值