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

数据准备

data

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")

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

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

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

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

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

ggplot2

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

library(tidyverse)

# 定义一种主题,方便后面重复使用

theme_boxplot

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

添加抖动散点

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

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

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

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

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

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

分组画箱线图

根据不同的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 % 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()

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

# 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()

ggpubr (带显著性的箱线图)

生成数据

# 均值为3,标准差为1的正态分布

c1

# Johnson分布的偏斜度2.2和峰度13

c2

# Johnson分布的偏斜度0和峰度20

c3

data

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

c("C_2", "C_3"),

c("C_1", "C_3"))

palette

# 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

使用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

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

箱线图多个y含组怎么画_箱线图的N种画法相关推荐

  1. 图象关于y轴对称是什么意思_关于x轴对称-图象关于y轴对称是什么函数

    就是以x为中心,图像两边对称 什么叫X轴对称?Y轴对称? X轴对称就是X不变Y 的值变成-Y相同的道理Y轴对称是Y不变X变成-X 关于x轴对称就是横坐标不变,纵坐标变相反数,如(2,3)关于x轴对称就 ...

  2. 图象关于y轴对称是什么意思_函数图象关于y轴对称是什么函数

    信息举报 时间:2020-12-24 本页为您甄选多篇描写函数图象关于y轴对称是什么函数,函数图象关于y轴对称是什么函数精选,函数图象关于y轴对称是什么函数大全,有议论,叙事 ,想象等形式.文章字数有 ...

  3. matlab画动物轮廓图,MATLAB一维插值的应用实例—画左右手的轮廓图

    问题提出 画你自己的手的形状,在MATLAB中输入 figure('position',get(0,'screensize')) axes('position',[0 0 1 1]) [x,y]=gi ...

  4. 图象关于y轴对称是什么意思_数学概念丨“图象”与“图像”是有区别的 ,你知道吗?...

    你与数学间 只差一个 公众号 解读教材教法,研究解题策略,传播数学文化,推广数学应用,推送知识干货,分享学习方法.精彩分享 初一数学全国各地期中试卷150套,word版 初二数学全国各地期中试卷150 ...

  5. 如何使用python画折线图-Python数据可视化:使用Python画柱状图和折线图

    Python爬虫太火了,没写过爬虫,都不敢说自己学过Python?! 可是刚一开始学我就遇到了难题----数据分析!听起来很高大上,有没有? 想要做爬虫,就得先学会使用数据分析工具,制作图表这是最基本 ...

  6. attention机制中的注意力图怎么画_注意力机制 | 图卷积多跳注意力机制 | Direct multihop Attention based GNN...

    导读:目前GNNs通过利用self-attention机制已经取得较好的效果.但目前的注意力机制都只是考虑到相连的节点,却不能利用到能提供图结构上下文信息的多跳邻居(multi-hop neighbo ...

  7. java架构图怎么画_架构模型图怎么画java

    逻辑视图 开发视图 过程视图 物理视图 场景视图 4+1视图提出后,业界也有其它的观点提出,诸如SEI(模块视图.组建和连接件视图.分配视图).西门子4种视图(概念.模块.代码.执行视图).以及RM- ...

  8. matlab黑白不同线形式,MATLAB:在黑白图像上画一条线

    你可能想看一下关于 adding a line to an image matrix的SO问题的 my answer.这是一个类似的例子,我在答案中有一个这样的例子,这将使一行从行和列索引(10,10 ...

  9. 经纬度绘图_【知识科普】地形图图例汇总,测绘人识图绘图必备(含dwg版下载)...

    地形图(topographic map)指的是地表起伏形态和地理位置.形状在水平面上的投影图.具体来讲,将地面上的地物和地貌按水平投影的方法(沿铅垂线方向投影到水平面上),并按一定的比例尺缩绘到图纸上 ...

  10. xmind各版本区别_思维导图工具 XMind 出了一个高颜值版:XMind ZEN

    XMind 对于思维导图的使用者来说不会陌生.作为一款优质的国产思维导图软件,它不仅有强大的功能,而且还可以同时在 macOS.Windows 和 Linux 上使用.不过,跨平台的特性也为软件带来了 ...

最新文章

  1. 第一次使用考试宝进行作业练习
  2. Lesson 15.1 学习率调度基本概念与手动实现方法
  3. python 匿名函数捕获变量值 (执行时的值)
  4. Python爬虫开发与项目实践
  5. cookie+memcached实现单点登陆
  6. JAVA基础知识之网络编程——-基于AIO的异步Socket通信
  7. Gartner预测:芯片短缺,十大汽车主机厂未来一半都将自主设计芯片
  8. 分析log及校准学习总结
  9. jquery 动画总结(主要指效果函数)
  10. DDD 到底什么鬼?
  11. 海思接收bt1120外同步视频流
  12. 回溯法高效搜索解空间树的两种办法
  13. Cartopy画地图第七天(python画浮雕地图和比例尺)
  14. 源码阅读之Splitter
  15. mysql怎么做排名并列_MySQL实现排名并查询指定用户排名功能,并列排名功能
  16. Ubuntu下使用opencv完成图像程序编程操作
  17. 针对Matlab脑电数据EEG处理、 eeglab工具箱、Neuracle数据采集操作要点事项
  18. Sass - 变量($)
  19. 火箭发射理论(基础篇-未完待续)//2021-1-27
  20. java沙漏_(java)五大常用算法

热门文章

  1. Cuda Graph (cuda 优化)
  2. 爱企人事工资管理系统 v8.1 免费下载
  3. 死磕算法!35篇算法设计实例+6本必读书打包送你
  4. linux从入门到精通(第2版)pdf
  5. oracle group concat
  6. 模糊数学 计算机智能,模糊数学与人智能技术.pdf
  7. turn.js教程及总结
  8. PDFLib9以上版本的去水印办法
  9. visio画图复制粘贴到word_visio复制粘贴到word中
  10. 前端页面,引入PingFang SC(苹方简)字体