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

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

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)
相关文章
|
6月前
|
机器学习/深度学习
这图怎么画| 相关性热图+柱状图
这图怎么画| 相关性热图+柱状图
62 0
|
6月前
|
人工智能 数据挖掘
这图怎么画| 气泡热图(基因表达泛癌分析)
这图怎么画| 气泡热图(基因表达泛癌分析)
70 0
|
8月前
|
数据可视化
R语言绘图教程丨Nature论文都在用的多组比较箱线图,自动计算显著性并标注,附带误差线
R语言绘图教程丨Nature论文都在用的多组比较箱线图,自动计算显著性并标注,附带误差线
R实战 | 对称云雨图 + 箱线图 + 配对散点 + 误差棒图 +均值连线
R实战 | 对称云雨图 + 箱线图 + 配对散点 + 误差棒图 +均值连线
1146 0
R实战 | 对称云雨图 + 箱线图 + 配对散点 + 误差棒图 +均值连线
|
12天前
R语言中绘制箱形图的替代品:蜂群图和小提琴图
R语言中绘制箱形图的替代品:蜂群图和小提琴图
17 0
|
6月前
|
数据挖掘
这图怎么画| 批量小提琴图+箱线图+散点+差异分析
这图怎么画| 批量小提琴图+箱线图+散点+差异分析
130 0
|
6月前
跟着 Cell 学作图 | 分组蜂群图+均值线+差异分析(组间+组内)
跟着 Cell 学作图 | 分组蜂群图+均值线+差异分析(组间+组内)
51 0
|
6月前
|
数据挖掘
跟着 NC 学作图 | 多组散点图+配对连线+差异分析
跟着 NC 学作图 | 多组散点图+配对连线+差异分析
71 0
|
6月前
|
数据挖掘
这图怎么画| 箱线图+散点+中位数连线
这图怎么画| 箱线图+散点+中位数连线
51 0
|
6月前
|
人工智能 数据可视化 数据挖掘
这图怎么画| 富集分析之双向柱状图
这图怎么画| 富集分析之双向柱状图
82 0