首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >箱线图的N种画法

箱线图的N种画法

作者头像
阿凡亮
发布2020-04-13 13:17:36
3K0
发布2020-04-13 13:17:36
举报
文章被收录于专栏:生物信息学生物信息学

箱形图(Box-plot)又称为盒须图、盒式图或箱线图,是一种用作显示一组数据分散情况资料的统计图。因形状如箱子而得名。在各种领域也经常被使用。

下图中标示了箱线图中每条线和点所表示的含义,应用到了分位数的概念。线主要表示五个数据节点,将一组数据从大到小排列,分别计算出他的上边缘(Maximum),上四分位数(Q3),中位数(Median),下四分位数(Q1),下边缘(Minimum)。不在上边缘与下边缘的范围内的为异常值,用点表示。

大家有没有觉得用箱型图表示显得即直观又美观呢,接下来小编介绍数种方式教你作图,总有一款适合你。

数据准备

data<- data.frame(Value= rnorm(300),                 Repeat= rep(paste("Repeat",1:3, sep ="_"),100),                 Condition= rep(c("Control","Test"),150))
>head(data)     Value Repeat Condition1 -1.1395507 Repeat_1 Control2 0.7319707 Repeat_2   Test3 -0.2219461 Repeat_3 Control4 -1.1454664 Repeat_1   Test5 1.0740937 Repeat_2 Control6 0.3741845 Repeat_3   Test

1

boxplot函数(R自带)

最方便的方法就是用boxplot函数,不需要依赖任何包

boxplot(data$Value, ylab="Value")

根据不同的条件,加上颜色

boxplot(Value~Condition, data=data,ylab="Value", col=c("darkred","darkgreen"))

多个分组(condition 和 repeat)的箱线图

boxplot(Value~Condition+Repeat, data=data,ylab="Value", col="darkgreen")

2

ggplot2

使用ggplot2来画箱线图是现在常用的方法

library(tidyverse)# 定义一种主题,方便后面重复使用theme_boxplot<- theme(panel.background=element_rect(fill="white", colour="black", size=0.25),     axis.line=element_line(colour="black", size=0.25),     axis.title=element_text(size=13, face="plain", color="black"),     axis.text = element_text(size=12, face="plain", color="black"),     legend.position="none"
# ggplot2画图ggplot(data, aes(Condition,Value)) +   geom_boxplot(aes(fill =Condition), notch = FALSE) +   scale_fill_brewer(palette ="Set2") +   theme_classic() + theme_boxplot

01

Part

01

添加抖动散点

ggplot(data, aes(Condition,Value)) +   geom_boxplot(aes(fill =Condition), notch = FALSE) +   geom_jitter(binaxis ="y", position = position_jitter(0.2), stackdir ="center", dotsize =0.4) +   scale_fill_brewer(palette ="Set2") +   theme_classic() + theme_boxplot

02

带凹槽(notched)的箱线图,中位数的置信区用凹槽表示

ggplot(data, aes(Condition,Value)) +   geom_boxplot(aes(fill =Condition), notch = TRUE, varwidth = TRUE) +   geom_jitter(binaxis ="y", position = position_jitter(0.2), stackdir ="center", dotsize =0.4) +   scale_fill_brewer(palette ="Set2") +   theme_classic() + theme_boxplot

03

比较流行的小提琴图,内嵌箱线图和扰动散点

ggplot(data, aes(Condition,Value)) +   geom_violin(aes(fill =Condition), trim = FALSE) +   geom_boxplot(width =0.2) +   geom_jitter(binaxis ="y", position = position_jitter(0.2), stackdir ="center", dotsize =0.4) +   scale_fill_brewer(palette ="Set2") +   theme_classic() + theme_boxplot

04

云雨图,它是密度分布图、箱线图、散点图的集合,完美的展示了所有数据信息

library(grid)
# GeomFlatViolin函数的定义见https://github.com/EasyChart/Beautiful-Visualization-with-Rggplot(data, aes(Condition,Value, fill=Condition)) +   geom_flat_violin(aes(fill =Condition), position = position_nudge(x=.25), color="black") +   geom_jitter(aes(color =Condition), width=0.1) +   geom_boxplot(width=.1, position=position_nudge(x=0.25), fill="white",size=0.5) +   scale_fill_brewer(palette ="Set2") +   coord_flip() + theme_bw() + theme_boxplot

02

Part

分组画箱线图

根据不同的Condition和Repeat对数据分组画图

ggplot(data, aes(Repeat,Value)) +   geom_boxplot(aes(fill =Condition), notch = FALSE, size =0.4) +   scale_fill_brewer(palette ="Set2") +   guides(fill=guide_legend(title="Repeat")) +   theme_bw()

同样的,我们可以对箱线图添加抖动点,但是分组之后,并不能直接添加抖动点,需要增加两列信息来辅助画抖动点

# 增加dist_cat和scat_adj ,用于画抖动点data<- data %>% mutate(dist_cat =as.numeric(Repeat),                       scat_adj= ifelse(Condition=="Control", -0.2,0.2))

# 增加之后的数据如下>head(data)     Value Repeat Condition dist_cat scat_adj1 -1.1395507 Repeat_1 Control       1    -0.22 0.7319707 Repeat_2   Test       2     0.23 -0.2219461 Repeat_3 Control       3    -0.24 -1.1454664 Repeat_1   Test       1     0.25 1.0740937 Repeat_2 Control       2    -0.26 0.3741845 Repeat_3   Test       3     0.2

ggplot(data, aes(Repeat,Value)) +   geom_boxplot(aes(fill =Condition), notch = FALSE, size =0.4) +   geom_jitter(aes(scat_adj+dist_cat,Value, fill = factor(Condition)),               position=position_jitter(width=0.1,height=0),               alpha=1,               shape=21, size =1.2) +   scale_fill_brewer(palette ="Set2") +   guides(fill=guide_legend(title="Condition ")) +   theme_bw()

小提琴图本来是由两个左右对称的密度估计曲线构成,那么对数据分组之后,我们可以只保留两个小提琴图的各一半,这样更能直接的观察出两组之间的差异!

# ggplot2并未提供这样的功能,这里定义了geom_split_violin函数来实现# geom_split_violin 的定义见 https://github.com/EasyChart/Beautiful-Visualization-with-Rggplot(data, aes(x =Repeat, y =Value, fill=Condition)) + geom_split_violin(draw_quantiles =0.5, trim = FALSE) +   geom_jitter(aes(scat_adj+dist_cat,Value, fill = factor(Condition)),               position=position_jitter(width=0.1,height=0),               alpha=1,               shape=21, size =1.2) + scale_fill_brewer(palette ="Set2") + guides(fill=guide_legend(title="Condition ")) + theme_bw()

3

ggpubr (带显著性的箱线图)

生成数据

# 均值为3,标准差为1的正态分布c1<- rnorm(100,3,1)# Johnson分布的偏斜度2.2和峰度13c2<- rJohnson(100, findParams(3,1,2.,13.1))# Johnson分布的偏斜度0和峰度20c3<- rJohnson(100, findParams(3,1,2.2,20))data<- data.frame( Conditon= rep(c("C_1","C_2","C_3"), each =100), Value= c(c1, c2, c3))

#数据如下>head(data) Conditon   Value1     C_1 2.6791692     C_1 1.6990263     C_1 5.4595684     C_1 3.7783655     C_1 3.6898816     C_1 1.295534

ggpubr的功能多样,它可以直接帮你画出箱线图、密度分布图、直方图、点图、偏差图,最重要的是画这些图的同时标上significance levels,使用起来也比较简单。这里主要介绍它的箱线图使用方法

使用ggboxplot函数来实现作图,并实现wilcox.test

1

library(ggpubr)library(RColorBrewer)
# 定义需要两两比较的组compaired<- list(c("C_1","C_2"),                 c("C_2","C_3"),                 c("C_1","C_3"))palette<- c(brewer.pal(7,"Set2")[c(1,2,4)])
# wilcox.testggboxplot(data, x ="Conditon", y ="Value",         fill="Conditon", palette = palette,         add="jitter", size=0.5) +   stat_compare_means(comparisons = compaired, method ="wilcox.test") +# 添加每两组变量的显著性   theme_classic() + theme_boxplot

使用ggplot2的语法添加显著性检验,并将wilcox.test 换成 t.test

2

# t.testggplot(data, aes(Conditon,Value))+ geom_boxplot(aes(fill =Conditon), notch = FALSE, outlier.alpha  =1) + scale_fill_brewer(palette ="Set2") + geom_signif(comparisons = compaired,             step_increase=0.1,             map_signif_level= F,             test= t.test) + theme_classic() + theme_boxplot
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-11-22,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 生物信息学 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档