这图怎么画| 多组箱线图+组间/内差异分析

简介: 这图怎么画| 多组箱线图+组间/内差异分析

box_cover

写在前面

【这图怎么画】系列的图都来自VIP群里同学的提问。推文只是对图片的复现,不代表作者对图片展现形式的认同。欢迎同学们在群里分享有意思的图片。

本期图片

Doi:https://doi.org/10.1111/nph.19115

相似图片:跟着Nat Commun学作图 | Post-hoc图(Extended error bar plot

复现结果

result


绘图

rm(list = ls())
library(ggplot2) 
library(ggpubr)
library(reshape2) 
library(dplyr) 
# data example
mat <- matrix(runif(40*12, min=0, max=10), nrow=40, ncol=12)
rownames(mat) <- paste0("sample", 1:40)
colnames(mat) <- paste0("gene", 1:12)
mat
gene_group = data.frame(gene = paste0("gene", 1:12),
                        group = rep(paste0("group", 1:6), each = 2))
sample_group = data.frame(sample = paste0("sample", 1:40),
                          group = rep(c('c','w'),20))
# data preprocess
library(reshape2)
mat_long <- melt(mat, varnames = c("sample", "gene"), value.name = "expression")
df1 <- merge(mat_long, gene_group, by = "gene")
df2 <- merge(mat_long, sample_group, by = "sample")
## calculate mean value
df1_mean = df1 %>% 
  group_by(gene) %>% 
  summarise(mean_value=mean(expression))
df1_mean$x = 1:12
df2_mean <- df2 %>%
  group_by(gene, group) %>%
  summarise(mean = mean(expression), sd = sd(expression))
df2_mean$gene <- factor(df2_mean$gene)
# plot
## left boxplot
df1$facet = 'facet1'# for facet title
p1 <- ggplot(df1,aes(gene,expression))+
  geom_boxplot(aes(fill=group,color=group),outlier.shape = 18,size=0.6)+
  stat_boxplot(aes(color=group),geom = "errorbar", width=0.3,size=0.6)+
  geom_segment(df1_mean,
               mapping=aes(x=x-0.5,xend=x+0.5,y=mean_value,yend = mean_value),
               color="white",size=0.5)+
  geom_hline(yintercept = 5, linetype = 2, color = "grey60",linewidth=0.8)+
  ## compare with gene6
  stat_compare_means(label = "p.signif", method = "t.test",
                     ref.group = "gene1",size = 5)+
  coord_flip()+
  scale_fill_manual(values = c("#e3ac6d","#9d7bb8","#6caf83","#d9586e","#3c74bb","#f85b2b"))+
  scale_color_manual(values = c("#e3ac6d","#9d7bb8","#6caf83","#d9586e","#3c74bb","#f85b2b"))+
  theme_bw()+
  theme(legend.position = "none",
        panel.grid = element_blank(),
        axis.text = element_text(color = "black",size=10))+
  labs(y="exp",x='')+
  annotate("rect", xmin = 0, xmax = 4.5, ymin = -Inf, ymax = Inf, alpha = 0.3,fill="#EAA9CF") +
  annotate("rect", xmin = 4.5, xmax = 8.5, ymin = -Inf, ymax = Inf, alpha = 0.3,fill="#D6BDFF") +
  annotate("rect", xmin = 8.5, xmax = 13, ymin = -Inf, ymax = Inf, alpha = 0.3,fill="#99DAFF")+
  facet_grid(~ facet)
p1
## right plot
df2_mean$facet = 'facet2' # for facet title
p2 <- ggplot(df2_mean,aes(x =gene, y = mean,
                          group = group,color = group)) +
  geom_point(position = position_dodge(0.8),shape=18,size=3)+
  geom_errorbar(aes(x =gene, y = mean,ymin = mean- sd, ymax = mean + sd,
                                       group = group,color = group), 
                width = 0,position = position_dodge(0.8),linewidth=0.5)+
  scale_color_manual(values = c("#7fc190","#efb684"))+
  stat_compare_means(df2, mapping = aes(gene,expression,group = group),
                     label = "p.signif", method = "t.test",size = 5)+
  coord_flip()+
  theme_bw()+
  theme(legend.position = c(0.9,0.95),
        legend.background = element_blank(),
        legend.key = element_blank(),
        panel.grid = element_blank(),
        axis.text.x = element_text(color = "black",size=10),
        axis.text.y = element_blank(),
        axis.ticks.y = element_blank())+
  labs(y='exp2',x='',color='')+
  annotate("rect", xmin = 0, xmax = 4.5, ymin = -Inf, ymax = Inf, alpha = 0.3,fill="#EAA9CF") +
  annotate("rect", xmin = 4.5, xmax = 8.5, ymin = -Inf, ymax = Inf, alpha = 0.3,fill="#D6BDFF") +
  annotate("rect", xmin = 8.5, xmax = 13, ymin = -Inf, ymax = Inf, alpha = 0.3,fill="#99DAFF")+
  facet_grid(~ facet)
p2
#merge
library(patchwork)
library(cowplot)
p1+p2+plot_layout(nrow= 1,
                  width = c(3, 2))
ggsave('boxplot.pdf',width = 10,height = 8)

往期内容

  1. 资源汇总 | 2022 木舟笔记原创推文合集(附数据及代码领取方式)
  2. CNS图表复现|生信分析|R绘图 资源分享&讨论群!
  3. R绘图 | 浅谈散点图及其变体的作图逻辑
  4. 这图怎么画| 有点复杂的散点图
  5. 这图怎么画 | 相关分析棒棒糖图
  6. 组学生信| Front Immunol |基于血清蛋白质组早期诊断标志筛选的简单套路
  7. (免费教程+代码领取)|跟着Cell学作图系列合集
  8. Q&A | 如何在论文中画出漂亮的插图?
  9. 跟着 Cell 学作图 | 桑葚图(ggalluvial)
  10. R实战 | Lasso回归模型建立及变量筛选
  11. 跟着 NC 学作图 | 互作网络图进阶(蛋白+富集通路)(Cytoscape)
  12. R实战 | 给聚类加个圈圈(ggunchull)
  13. R实战 | NGS数据时间序列分析(maSigPro)
  14. 跟着 Cell 学作图 | 韦恩图(ggVennDiagram)
相关文章
|
数据挖掘
跟着 Nature 学作图 | 相关性热图(显示相关性散点图)
跟着 Nature 学作图 | 相关性热图(显示相关性散点图)
1210 0
R实战 | 对称云雨图 + 箱线图 + 配对散点 + 误差棒图 +均值连线
R实战 | 对称云雨图 + 箱线图 + 配对散点 + 误差棒图 +均值连线
2194 1
R实战 | 对称云雨图 + 箱线图 + 配对散点 + 误差棒图 +均值连线
|
数据可视化
R语言绘图教程丨Nature论文都在用的多组比较箱线图,自动计算显著性并标注,附带误差线
R语言绘图教程丨Nature论文都在用的多组比较箱线图,自动计算显著性并标注,附带误差线
|
数据采集 JSON 数据挖掘
如何利用Python实现高效数据清洗
数据清洗是数据分析过程中不可避免的一步。在大规模数据处理任务中,手动数据清洗会耗费大量时间和资源。本文介绍了如何使用Python编写脚本来自动化数据清洗,以提高数据分析的效率。
409 3
|
运维 监控 安全
物联网卡:物联网卡为什么不能使用在手机上
物联网卡(IoT SIM卡)通常是为物联网设备设计的,这些设备包括但不限于智能家居设备、可穿戴设备、工业监控设备等。它们与用于智能手机的SIM卡有所不同,主要是因为设计目标、功能限制、资费结构以及网络接入策略上的差异。以下是物联网卡不能直接在手机上使用的主要原因:
|
存储 自然语言处理 算法
知识图谱算法有哪些
知识图谱是以图结构描述的知识。与传统数据库相比,知识图谱在存储、查询、检索方面具有诸多优势。传统数据库对数据的组织是以字段为单位,而知识图谱通过关系、属性和实体等数据类型,将数据组织成复杂的图,使其更容易理解。
|
数据采集 存储 人工智能
蚂蚁集团联合上海仁济医院泌尿科发布国内首个临床专科推理数据集:RJUA-QA
详细介绍数据集的构建过程、特点及统计分析,并全面评测了行业和通用大模型在该数据集上的性能,后续团队将持续优化数据集,为人工智能在医疗领域的研究与应用提供有力支持。
蚂蚁集团联合上海仁济医院泌尿科发布国内首个临床专科推理数据集:RJUA-QA
|
应用服务中间件 Linux nginx
Linux操作系统上用Docker部署Nginx
Linux操作系统上用Docker部署Nginx
508 2
Linux操作系统上用Docker部署Nginx
Typora 主题,设置代码块Mac风格三个小圆点
Typora 编辑器让人们能更简单地用**Markdown语言**书写文字,解决了使用传统的Markdown编辑器写文的痛点,并且界面简洁优美,实现了实时预览等功能。
1164 1
|
前端开发 容器
react使用echarts图表
react使用echarts图表
529 0