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

357e939e1d0dd9a5ede40b67c8dc7872.jpeg

box_cover

写在前面

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

本期图片

e0b33369393b83a53bafb74c816934b6.png

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

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

复现结果

b5867699b281efc8436519c711b5fd9a.png
result

可在AI上微调。使用代码时请按需修改,切勿全部套用。

示例数据和代码领取

木舟笔记永久VIP企划

「权益:」

  1. 「木舟笔记所有推文示例数据及代码(「在VIP群里」实时更新」)。

    4dbf42bb699b81abd82dc4a1a5749b0f.png
    data+code
  2. 木舟笔记「科研交流群」

「收费:」

「169¥/人」。可添加微信:mzbj0002 转账(或扫描下方二维码),或直接在文末打赏。木舟笔记「2022VIP」可直接支付「70¥」升级。

点赞在看 本文,分享至朋友圈集赞30个保留30分钟,可优惠20¥

bafbbe2ecacb06e216ea3a7fc25b8a0e.png

绘图

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)


a7b8be2445bf97182af827d46da40c0f.png
木舟笔记矩阵
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值