box_cover
写在前面
【这图怎么画】系列的图都来自VIP群
里同学的提问。推文只是对图片的复现,不代表作者对图片展现形式的认同。欢迎同学们在群里分享有意思的图片。
本期图片
❝ ❞
相似图片:跟着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)
往期内容
- 资源汇总 | 2022 木舟笔记原创推文合集(附数据及代码领取方式)
- CNS图表复现|生信分析|R绘图 资源分享&讨论群!
- R绘图 | 浅谈散点图及其变体的作图逻辑
- 这图怎么画| 有点复杂的散点图
- 这图怎么画 | 相关分析棒棒糖图
- 组学生信| Front Immunol |基于血清蛋白质组早期诊断标志筛选的简单套路
- (免费教程+代码领取)|跟着Cell学作图系列合集
- Q&A | 如何在论文中画出漂亮的插图?
- 跟着 Cell 学作图 | 桑葚图(ggalluvial)
- R实战 | Lasso回归模型建立及变量筛选
- 跟着 NC 学作图 | 互作网络图进阶(蛋白+富集通路)(Cytoscape)
- R实战 | 给聚类加个圈圈(ggunchull)
- R实战 | NGS数据时间序列分析(maSigPro)
- 跟着 Cell 学作图 | 韦恩图(ggVennDiagram)