FigDraw 6. SCI 文章绘图之箱线图 (Boxplot)

图片

点击关注,桓峰基因

桓峰基因

生物信息分析,SCI文章撰写及生物信息基础知识学习:R语言学习,perl基础编程,linux系统命令,Python遇见更好的你

109篇原创内容

公众号

桓峰基因的教程不但教您怎么使用,还会定期分析一些相关的文章,学会教程只是基础,但是如果把分析结果整合到文章里面才是目的,觉得我们这些教程还不错,并且您按照我们的教程分析出来不错的结果发了文章记得告知我们,并在文章中感谢一下我们哦!

公司英文名称:Kyoho Gene Technology (Beijing) Co.,Ltd.

如果您觉得这些确实没基础,需要专业的生信人员帮助分析,直接扫码加微信nihaoooo123,我们24小时在线!!

前两期简单介绍了 R 语言基础,比较简单粗略,然后介绍了 R 语言中表格的转换,因为现在绘图基本以及舍弃了基本绘图的方式,都会选择 ggplot2 来作图,这期SCI绘图介绍一下箱线图!


前 言

箱形图(Box-plot)又称为盒须图、盒式图或箱线图,是一种用作显示一组数据分散情况资料的统计图。因形状如箱子而得名。在各种领域也经常被使用,常见于品质管理。它主要用于反映原始数据分布的特征,还可以进行多组数据分布特征的比 较。箱线图的绘制方法是:先找出一组数据的上边缘、下边缘、中位数和两个四分位数;然后, 连接两个四分位数画出箱体;再将上边缘和下边缘与箱体相连接,中位数在箱体中间

箱形图的优点

  1. 直观明了地识别数据中的异常值;

  2. 利用箱线图判断数据批的偏态和尾重。

局限性

  1. 不能精确地衡量数据分布的偏态和尾重程度;

  2. 对于批量比较大的数据,反映的信息更加模糊以及用中位数代表总体评价水平有一定的局限性

图片

基础参数


本教程介绍了如何使用 R 软件和 ggplot2 包创建箱线图。需要使用函数 geom_boxplot()。一个简化的格式是:

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

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

notch:逻辑值。如果为TRUE,则制作一个带缺口的箱线图。缺口显示围绕中位数的置信区间,通常基于中位数 +/- 1.58*IQR/sqrt(n)。缺口用于比较组;如果两个框的凹口不重叠,这是中位数不同的有力证据。

1. 软件包安装

安装ggplot2即可,其他软件包为处理数据使用。

if (!require(ggplot2)) install.packages("ggplot2")

library(ggplot2)

2. 数据读取

data(mpg)
# 将class变量转变为因子变量
mpg$class = as.factor(mpg$class)
head(mpg)
## # A tibble: 6 x 11
##   manufacturer model displ  year   cyl trans      drv     cty   hwy fl    class 
##   <chr>        <chr> <dbl> <int> <int> <chr>      <chr> <int> <int> <chr> <fct> 
## 1 audi         a4      1.8  1999     4 auto(l5)   f        18    29 p     compa~
## 2 audi         a4      1.8  1999     4 manual(m5) f        21    29 p     compa~
## 3 audi         a4      2    2008     4 manual(m6) f        20    31 p     compa~
## 4 audi         a4      2    2008     4 auto(av)   f        21    30 p     compa~
## 5 audi         a4      2.8  1999     6 auto(l5)   f        16    26 p     compa~
## 6 audi         a4      2.8  1999     6 manual(m5) f        18    26 p     compa~
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       : Factor w/ 7 levels "2seater","compact",..: 2 2 2 2 2 2 2 2 2 2 ...

3. 简单箱线图

ggplot(mpg, aes(class, hwy)) + geom_boxplot() + theme_bw()

图片

4. 水平箱线图

水平箱线图只需要旋转坐标轴,即 + coord_flip(),如下:

ggplot(mpg, aes(class, hwy)) + geom_boxplot() + coord_flip() + theme_bw()

图片

5. 绘制有缺口的箱线图

1. 简单带有缺口的箱线图

ggplot(mpg, aes(class, hwy)) + geom_boxplot(notch = TRUE) + theme_bw()

图片

2. 修改离群值的颜色形状和大小

ggplot(mpg, aes(class, hwy)) + geom_boxplot(notch = TRUE, outlier.colour = "red",
    outlier.shape = 6, outlier.size = 3) + theme_bw()

图片

6. 将均值点加入mean

函数 stat_summary() 可用于将平均点添加到箱线图中:

ggplot(mpg, aes(class, hwy)) + geom_boxplot() + stat_summary(fun.y = mean, geom = "point",
    shape = 23, size = 4) + theme_bw()

图片

7. 选择要显示的项目

ggplot(mpg, aes(class, hwy)) + geom_boxplot() + scale_x_discrete(limits = c("compact",
    "suv", "midsize")) + theme_bw()

图片

8. 箱线图描点

可以使用函数 geom_dotplot() 或 geom_jitter() 将点添加到箱线图中。

1. geom_dotplot()

# Box plot with dot plot
ggplot(mpg, aes(class, hwy)) + geom_boxplot() + geom_dotplot(binaxis = "y", stackdir = "center",
    dotsize = 0.5) + theme_bw()

图片

2. geom_jitter()

# Box plot with jittered points 0.2 : x 方向的抖动程度
ggplot(mpg, aes(class, hwy)) + geom_boxplot() + geom_jitter(shape = 16, position = position_jitter(0.2)) +
    theme_bw()

图片

9. 按分组修改箱线图颜色

修改箱线图线的颜色,可以使用以下函数手动更改箱线图的颜色:

  1. scale_color_manual() : 使用自定义颜色

  2. scale_color_brewer() :使用来自 RColorBrewer 包的调色板

  3. scale_color_grey() :使用灰色调色板

边框颜色:color()

mpg$year = factor(mpg$year)
ggplot(mpg, aes(x = class, y = hwy, color = year)) + geom_boxplot() + theme_bw()

图片

更换不同的颜色

p = ggplot(mpg, aes(class, hwy, color = year)) + geom_boxplot() + theme_bw()
p1 = p + scale_color_manual(values = c("#999999", "#E69F00", "#56B4E9"))
p2 = p + scale_color_brewer(palette = "Dark2")
p3 = p + scale_color_grey() + theme_classic()

library(patchwork)
(p | p1)/(p2 | p3)

图片

修改箱线图的填充颜色

# 使用单一颜色
p4 = ggplot(mpg, aes(class, hwy)) + geom_boxplot(fill = "#A4A4A4", color = "black") +
    theme_classic()
# 根据分组填充不同的颜色
p5 = ggplot(mpg, aes(class, hwy, fill = class)) + geom_boxplot()

p4 | p5 + theme_bw()

图片

10. 修改图例位置和顺序

通过修改legend.position()改变图例的位置,包括top,bottom,none,如下:

ggplot(mpg,aes(class, hwy,fill=year))+
  geom_boxplot() + theme_bw()+
  theme(legend.position = "top") + # 修改图例位置("top/bottom/none")
  scale_x_discrete(limits = c("compact", "suv","midsize"))  # 修改图例顺序

图片

11. 多组箱线图

多组箱线图根据不同的需求可以调整颜色,描点,以及改变点的位置等操作,如下:

# Change box plot colors by groups
p1 <- ggplot(mpg, aes(x = class, y = hwy, fill = year)) + geom_boxplot() + theme_bw()

# Change the position
p2 <- ggplot(mpg, aes(x = class, y = hwy, fill = year)) + geom_boxplot(position = position_dodge(1)) +
    theme_bw()
# 描点
p3 <- ggplot(mpg, aes(x = class, y = hwy, fill = year)) + geom_boxplot(position = position_dodge(1)) +
    geom_dotplot(binaxis = "y", stackdir = "center", position = position_dodge(1)) +
    theme_bw()

p4 <- ggplot(mpg, aes(class, hwy, fill = year)) + geom_boxplot() + geom_jitter(shape = 16,
    position = position_jitter(0.5)) + theme_bw()

# 修改颜色
p5 <- ggplot(mpg, aes(class, hwy, fill = year)) + geom_boxplot() + theme_bw() + scale_fill_manual(values = c("#999999",
    "#E69F00", "#56B4E9")) 

(p1|p2)/(p3|p4)/p5

图片

12. 添加p值和显著性

这里涉及到两种两组数据比较使用的统计学方法,包括:

  1. wilcox.test(秩合检验)

  2. t.test检验

多组数据比较使用的统计学,包括:

  1. Anova分析

  2. Kruskal-Wallis检验

library(tidyverse)
library(reshape2)
library(ggpubr)
library(rstatix)
library(ggsci)
library(ggsignif)

1. 两组数据比较分析

这个我们改成使用iris数据集,读取数据,如下:

data(iris)
head(iris)
##   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1          5.1         3.5          1.4         0.2  setosa
## 2          4.9         3.0          1.4         0.2  setosa
## 3          4.7         3.2          1.3         0.2  setosa
## 4          4.6         3.1          1.5         0.2  setosa
## 5          5.0         3.6          1.4         0.2  setosa
## 6          5.4         3.9          1.7         0.4  setosa

1. Wilcox(秩合检验)

Wilcoxon秩和检验(Wilcoxon rank sum test)用于推断计量资料或等级资料的两个独立样本所来自的两个总体分布位置是否有差别。无论两个总体分布的形状有无差别,秩和检验的目的是推断两个总体分布的位置是否有差别,这正是实践中所需要的,如要推断两个不同人群的某项指标值的大小是否有差别或哪个人群的大,可用其指标值分布的位置差别反映,而不关心其指标值分布的形状有无差别。

两样本比较的秩和检验的基本思想是:如果待比较的两样本(样本含量分别为n1及n2)来自位置相同的两个总体(即H0成立),则含量为n1的样本之实际秩和T与其理论秩和n1 (N+1)/2之差[T- n1 (N+1)/2]纯系抽样误差所致,故此差值一般不会很大,而差值越大的概率越小。若从现有样本中算得的T与其理论秩和相差很大,则说明从H0规定的总体中随机抽得现有样本及更极端样本的概率P很小,如小于等于检验水准a,则可拒绝H0。本篇文章将举例介绍Wilcoxon秩和检验的假设检验理论。

comparisons = list(c("versicolor", "virginica"), c("versicolor", "setosa"), c("virginica",
    "setosa"))

ggplot(iris, aes(Species, Sepal.Length, fill = Species)) + geom_boxplot() + geom_jitter(shape = 16,
    size = 2, position = position_jitter(0.2)) + geom_signif(comparisons = comparisons,
    map_signif_level = T, textsize = 6, test = wilcox.test, step_increase = 0.2) +
    guides(fill = FALSE) + xlab(NULL) + theme_classic()

图片

2. T检验

t检验,亦称student t检验(Student’s t test),主要用于样本含量较小(例如n < 30),总体标准差σ未知的正态分布。[1] t检验是用t分布理论来推论差异发生的概率,从而比较两个平均数的差异是否显著。

ggplot(iris, aes(Species, Sepal.Length, fill = Species)) + geom_boxplot() + scale_fill_jco() +
    geom_jitter(shape = 16, size = 2, position = position_jitter(0.2)) + geom_signif(comparisons = list(c("versicolor",
    "virginica"), c("versicolor", "setosa"), c("virginica", "setosa")), map_signif_level = T,
    textsize = 6, test = t.test, step_increase = 0.2) + guides(fill = F) + xlab(NULL) +
    theme_classic()

图片

2. 多组数据比较分析

1. Anova分析

方差分析是英国统计学家R.A.Fisher提出的对两个或多个样本平均数差异显著性检验的方法。它的基本思想是将测量数据的总变异(即总方差)按照变异来源分为处理(组间)效应和误差(组内)效应,并作出其数量估计,从而确定实验处理对研究结果影响力的大小。方差分析的步骤为:总平方和分解、总自由度分解和F检验。若F检验显著,则可以进行多重比较,从而发现哪些处理具有两两间的差异。

ggplot(iris, aes(Species, Sepal.Length, fill = Species)) + geom_boxplot() + scale_fill_jco() +
    geom_jitter(shape = 16, size = 2, position = position_jitter(0.2)) + stat_compare_means(method = "anova") +
    theme_bw()

图片

2. Kruskal-Wallis检验

我们在做方差分析时,数据是需要满足正态性和方差齐性两个条件的,而在我们的实际工作中经常碰到数据是偏态分布的、多个组中有一组或者几组数据的方差太大或者太小、观察的数据是有序分类变量等情况,因此方差分析分析并不适合,需要采用非参数检验的方法,这时我们可以采用Kruskal-Wallis检验。Kruskal-Wallis检验的思想是把n组样本混合起来成为一个数据集(即假设他们是来自同一个样本),然后将数据从小到大编秩,每个数据在混合数据集中都有自己的秩;如果顺序位数相同,则取平均值作为秩。再然后求各组的平均秩次,如果这n组数据来自同一个样本,则应该各组的秩次和混合数据的总平均秩次相差不大,如果差异很大的话,则说明各组不是来自同一个总体。

ggplot(iris, aes(Species, Sepal.Length, fill = Species)) + geom_boxplot() + scale_fill_jco() +
    geom_jitter(shape = 16, size = 2, position = position_jitter(0.2)) + stat_compare_means() +
    theme_bw()

图片

13. 多组数据进行分面

当我们分析数据时,需要把每个变量分割出来,仔细观察时,就需要进行分面。分面常用函数facet_grid(),即可实现。

1. 添加P值

我们可以对每组变量不同分类的差距进行P值的绘制,如下:

iris %>% melt() %>%
  ggplot(aes(variable,value,fill=Species)) + 
  geom_boxplot()+
  scale_fill_jco()+
  stat_compare_means(label = "p.format")+
  facet_grid(.~variable,scales = "free",space="free_x")+
  theme_bw()+theme(axis.text.x = element_blank())

图片

2. 添加显著性

同样,我们也可以对每个变量的不同分组的显著性进行绘制,如下:

iris %>% melt() %>%
ggplot(aes(variable,value,fill=Species)) + 
  geom_boxplot()+
  scale_fill_jco()+
  stat_compare_means(label =  "p.signif", label.x = 1.5)+
  facet_grid(.~variable,scales = "free",space="free_x")+
  theme_bw()+theme(axis.text.x = element_blank())

图片

总结了这么多,是否对Boxplot怎么搞清晰多了,文章中可能出现的图形都已经包含在细节中,您细品,仔细品就豁然开朗了!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值