N种箱线图的画法

image.png

图中标示了箱线图中每条线和点表示的含义,其中应用到了分位数的概念
线的主要包含五个数据节点,将一组数据从大到小排列,分别计算出他的上边缘(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 Condition
1 -1.1395507 Repeat_1   Control
2  0.7319707 Repeat_2      Test
3 -0.2219461 Repeat_3   Control
4 -1.1454664 Repeat_1      Test
5  1.0740937 Repeat_2   Control
6  0.3741845 Repeat_3      Test

boxplot函数(R自带)

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

boxplot(data$Value, ylab="Value")
image.png

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

boxplot(Value ~ Condition, data=data, ylab="Value", col=c("darkred", "darkgreen"))
boxplot(Value ~ Condition * Repeat, data=data, ylab="Value", col="darkgreen")
image.png

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

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

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
image.png

添加抖动散点

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
image.png

带凹槽(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
image.png

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

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
image.png

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

library(grid)

# GeomFlatViolin函数的定义见https://github.com/EasyChart/Beautiful-Visualization-with-R
ggplot(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

image.png

分组画箱线图

根据不同的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()
image.png

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

# 增加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_adj
1 -1.1395507 Repeat_1   Control        1     -0.2
2  0.7319707 Repeat_2      Test        2      0.2
3 -0.2219461 Repeat_3   Control        3     -0.2
4 -1.1454664 Repeat_1      Test        1      0.2
5  1.0740937 Repeat_2   Control        2     -0.2
6  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()
image.png

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

# ggplot2并未提供这样的功能,这里定义了geom_split_violin函数来实现
# geom_split_violin 的定义见 https://github.com/EasyChart/Beautiful-Visualization-with-R
ggplot(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()
image.png

ggpubr (带显著性的箱线图)

生成数据

# 均值为3,标准差为1的正态分布
c1 <- rnorm(100, 3, 1)
# Johnson分布的偏斜度2.2和峰度13
c2 <- rJohnson(100, findParams(3, 1, 2., 13.1))
# Johnson分布的偏斜度0和峰度20
c3 <- 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    Value
1      C_1 2.679169
2      C_1 1.699026
3      C_1 5.459568
4      C_1 3.778365
5      C_1 3.689881
6      C_1 1.295534

ggpubr的功能多样,这里只举箱线图的例子

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.test
ggboxplot(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
image.png

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

# t.test
ggplot(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
image.png

欢迎关注公众号:"生物信息学"

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 229,836评论 6 540
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 99,275评论 3 428
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 177,904评论 0 383
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 63,633评论 1 317
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 72,368评论 6 410
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 55,736评论 1 328
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 43,740评论 3 446
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 42,919评论 0 289
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 49,481评论 1 335
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 41,235评论 3 358
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 43,427评论 1 374
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 38,968评论 5 363
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 44,656评论 3 348
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 35,055评论 0 28
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 36,348评论 1 294
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 52,160评论 3 398
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 48,380评论 2 379

推荐阅读更多精彩内容