截断堆积柱状图

本教程原文链接:截断堆积柱状图绘制教程

欢迎大家转载!!!!

本期教程

写在前面

堆积柱状图是柱状图的常见类型之一,也是平时使用概率较高的图形之一。我们前期发布了很多个柱状图的绘制教程,若你刚兴趣可以自行查看。堆积柱状图,最近也发布了一个相对详细的教程,基于R语言 | 绘制个性化堆积柱状图

对于截断图的绘制,也是相对比较简单,因为我们柱状图和堆积柱状图都是基于ggplot2绘制。因此,基本全部的代码都是可以通用。我们次教程,也是基于前期的教程进行修改即可。

截断堆积柱状图绘制

导入所需的R包和数据

R 复制代码
library(ggplot2) 
library(cowplot)
library(dplyr)
library(tidyr)
library(ggbreak)
library(ggsignif)
library(ggpubr)
library(EnvStats)
library(stats)

导入数据

复制代码
请结合自己的数据进行导入!!!

我们这里需要长数据,需要将宽数据转换成长数据

R 复制代码
data_df <- gather(data, key = "Group", value = "Value", -Category)
data_df

绘制基础图形

R 复制代码
# 绘制柱状堆积图
ggplot(data_df, aes(Category, y = Value , fill = Group))+
  #geom_bar(stat = "identity", position = "fill")+
  geom_bar(stat = "identity", position = "stack")+
  scale_y_continuous(expand = c(0, 0), 
                     labels = scales::number_format(accuracy = 1)  ## 设置Y轴保留一位小数点
                     )+
  scale_x_discrete(expand = c(0,0.5))

我们可以自行先设置主题,以便于后期的图形绘制。

R 复制代码
mytheme01 <- theme_classic()+
  theme(axis.line = element_line(size = 1),  ## 粗细
        text=element_text(family = "sans",colour ="black",size = 12),
        axis.text.x = element_text(color = "black", size = 12),
        axis.text.y = element_text(color = "black",size = 12),
        axis.ticks = element_line(size = 0.6,colour = "black"),
        axis.ticks.length = unit(1.5,units = "mm"),
        #legend.position = "none",
        strip.background = element_blank()
       )
R 复制代码
mytheme02 <- theme_classic() +
  theme(
    text = element_text(family = "sans", colour = "gray30", size = 12),
    axis.line = element_line(size = 0.6, colour = "gray30"),
    axis.ticks = element_line(size = 0.6, colour = "gray30"),
    axis.ticks.length = unit(1.5, units = "mm"),
    plot.margin = unit(c(1, 1, 1, 1), units = "inches")
  )

图形修改和截断设置

R 复制代码
ggplot(data_df, aes(Category, y = Value , fill = Group))+
  #geom_bar(stat = "identity", position = "fill")+
  geom_bar(stat = "identity", position = "stack")+
  scale_y_continuous(expand = c(0, 0), 
                     labels = scales::number_format(accuracy = 1)  ## 设置Y轴保留一位小数点
                     )+
  scale_x_discrete(expand = c(0,0.5))+
  scale_y_break(c(33,70),
                  scales = "free", #'fixed', 'free'
                  )+

  ## 设置颜色
  scale_fill_manual(values = c("#386cb0", "#fdc086", "#1b9e77","#a6cee3","#bebada","#e5c494"))+
  labs(x = "Season", y = "Percentage", fill = NULL) +
  theme_classic()

堆积柱状图柱子参数修改

R 复制代码
p1 <- ggplot(data_df, aes(Category, y = Value , fill = Group))+
  #geom_bar(stat = "identity", position = "fill")+
  geom_bar(stat = "identity", 
           position = "stack",
           color = "black", ## 柱边颜色
           width = 0.5,    ## 柱子宽度
           size = 1.0     ## 粗细 
           )+
  scale_y_continuous(expand = c(0, 0), 
                     labels = scales::number_format(accuracy = 1)  ## 设置Y轴保留一位小数点
                     )+
  scale_x_discrete(expand = c(0,0.5))+
  scale_y_break(c(33,70),
    scales = "free", #'fixed', 'free'
    expand = c(0, 0),
    ## 修改坐标轴
    ticklabels = c(70,90,110,120),
    space = 0.25
                  )+

  ## 设置颜色
  scale_fill_manual(values = c("#386cb0", "#fdc086", "#1b9e77","#a6cee3","#bebada","#e5c494"))+
  labs(x = NULL, y = "Number of DEGs", fill = NULL) +
  theme_classic()

设置主题参数

R 复制代码
p1 +

  ## 设置颜色
  scale_fill_manual(values = c("#386cb0", "#fdc086", "#1b9e77","#a6cee3","#bebada","#e5c494"))+
  labs(x = NULL, y = "Number of DEGs", fill = NULL) +
  # 设置主题
  theme_classic()+
  theme(axis.line = element_line(size = 1),  ## 粗细
        text=element_text(family = "sans",colour ="black",size = 12),
        axis.text.x = element_text(color = "black", size = 12),
        axis.text.y = element_text(color = "black",size = 12),
        axis.ticks = element_line(size = 1,colour = "black"),
        axis.ticks.length = unit(1.5,units = "mm"),
        legend.position = "none",
        strip.background = element_blank()
       )

添加不同的截断位置

R 复制代码
ggplot(data_df, aes(Category, y = Value , fill = Group))+
  #geom_bar(stat = "identity", position = "fill")+
  geom_bar(stat = "identity", 
           position = "stack",
           color = "black", ## 柱边颜色
           width = 0.5,    ## 柱子宽度
           size = 1.0     ## 粗细 
           )+
  scale_y_continuous(expand = c(0, 0), 
                     labels = scales::number_format(accuracy = 1)  ## 设置Y轴保留一位小数点
                     )+
  scale_x_discrete(expand = c(0,0.5))+
  ##'@第一个截断位置
  scale_y_break(c(5,20),
                scales = "free",
                ticklabels = c(20,25,30))+
  ##'@第二个截断位置
  scale_y_break(c(30,70),
    scales = "free", #'fixed', 'free'
    # ## 修改坐标轴
    ticklabels = c(70,90,110,120),
    space = 0.25,
    expand = c(0, 0))+
  ##'@第三个截断位置
  # scale_y_break(c(90,105),
  #               scales = "free",
  #               expand = c(0, 0))+
  ## 设置颜色
  scale_fill_manual(values = c("#386cb0", "#fdc086", "#1b9e77","#a6cee3","#bebada","#e5c494"))+
  labs(x = NULL, y = "Number of DEGs", fill = NULL) +
  # 设置主题
  theme_classic()+
  theme(axis.line = element_line(size = 1),  ## 粗细
        text=element_text(family = "sans",colour ="black",size = 12),
        axis.text.x = element_text(color = "black", size = 12),
        axis.text.y = element_text(color = "black",size = 12),
        axis.ticks = element_line(size = 1,colour = "black"),
        axis.ticks.length = unit(1.5,units = "mm"),
        legend.position = "none",
        strip.background = element_blank()
       )

百分比堆积柱状图进行截断设置

复制代码
ggplot(data_df, aes(Category, y = Value , fill = Group))+
  geom_bar(stat = "identity", position = "fill")+
  scale_y_continuous(expand = c(0, 0), 
                     labels = scales::number_format(accuracy = 0.1)  ## 设置Y轴保留一位小数点
                     )+
  scale_x_discrete(expand = c(0,0.5))+
  ##'@第一个截断位置
  scale_y_break(c(0.3,0.5),
                scales = "free",
                ticklabels = c(0.5,0.8,1.0),
                expand = c(0,0))+
  scale_fill_manual(values = c("#386cb0", "#fdc086", "#1b9e77","#a6cee3","#bebada","#e5c494"))+
  labs(x = NULL, y = "Number of DEGs", fill = NULL) +
  # 设置主题
  theme_classic()+
  theme(axis.line = element_line(size = 1),  ## 粗细
        text=element_text(family = "sans",colour ="black",size = 12),
        axis.text.x = element_text(color = "black", size = 12),
        axis.text.y = element_text(color = "black",size = 12),
        axis.ticks = element_line(size = 1,colour = "black"),
        axis.ticks.length = unit(1.5,units = "mm"),
        legend.position = "none",
        strip.background = element_blank()
       )

本教程原文链接:截断堆积柱状图绘制教程

欢迎大家转载!!!!
小杜的生信笔记 ,自2021年11月开始做的知识分享,主要内容是R语言绘图教程转录组上游分析转录组下游分析等内容。凡事在社群同学,可免费获得自2021年11月份至今全部教程,教程配备事例数据和相关代码,我们会持续更新中。

往期教程部分内容











往期部分文章

1. 复现SCI文章系列专栏

2. 《生信知识库订阅须知》,同步更新,易于搜索与管理。

3. 最全WGCNA教程(替换数据即可出全部结果与图形)


4. 精美图形绘制教程

5. 转录组分析教程

6. 转录组下游分析

小杜的生信筆記 ,主要发表或收录生物信息学的教程,以及基于R的分析和可视化(包括数据分析,图形绘制等);分享感兴趣的文献和学习资料!!

相关推荐
橙子牛奶糖1 天前
2025-11-27-Nature Genetics 本周最新文献速递
gwas·生物信息学·单细胞测序
小杜的生信筆記3 天前
SNP曼哈顿图绘制
生物信息学·snp·中科院top期刊·曼哈顿图
Solyn_HAN11 天前
生信项目管理与版本控制进阶:Git Flow+Zenodo+ReadMe 规范(科研项目可复现实操)
生物信息学
Solyn_HAN11 天前
非编码 RNA(ceRNA/lncRNA/circRNA)分析完整流程:从数据下载到功能验证(含代码模板)
python·bash·生物信息学·r
Solyn_HAN11 天前
多组学可视化进阶:OmicsDashboard 搭建与交互式报告生成(R Shiny/Python Dash 实战)
生物信息学
Solyn_HAN14 天前
Snakemake 从入门到实战:生信自动化工作流搭建指南
生物信息学·snakemake
Solyn_HAN14 天前
Python 生信进阶:Biopython 库完全指南(序列处理 + 数据库交互)
python·生物信息学·biopython
橙子牛奶糖1 个月前
Nature | 本周最新文献速递
gwas·生物信息学·单细胞测序
czliutz2 个月前
R绘制股票日波动线图 中国海油600938
开发语言·r语言·r语言绘图
陈天白2 个月前
RNA-seq分析之最佳cutoff(TCGA版)
r语言·生物信息学·rna-seq