You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在R的ggplot中调整Y轴间距,凸显低值氮曲线动态?

问题描述

我绘制了多种氮类型的曲线图,其中两种在水体中的检测浓度远低于另一种。我希望观察红线与蓝线之间的动态变化,但由于绿线数值过高,Y轴被过度拉伸,导致0-0.5区间的规律难以看清。需要调整Y轴区间,展示0-0.5区间内的变化规律,类似实现Y轴断裂(Y-break)的效果。

当前代码
sampleSites <- list("BVR1", "BVR2", "BVR3", "BVR4","BVR5", "BVR6")

samplePlotList <- list()


for (site in sampleSites){
  dat <- subset(nitrogen, ID==site)
  samplePlotList[[site]] <-
    dat %>%
    ggplot(aes_string(x="Month", y="value", group="condition")) +
    geom_line(size=0.5, aes(colour=condition)) +
    theme_classic() +
    theme(legend.position='none') +
    theme(text=element_text(size=10, family='Calibri')) +
    theme(axis.text=element_text(size=10, family='Calibri')) +
    labs(x= NULL) +
    ylim(0, 9) +
    theme(plot.margin = margin(0.75,0,0.5,0, "cm"))
}
数据框头部
structure(list(ID = structure(1:6, levels = c("BVR1", "BVR2", 
"BVR3", "BVR4", "BVR5", "BVR6"), class = "factor"), Survey = c(1L, 
1L, 1L, 1L, 1L, 1L), Month = structure(c(1L, 1L, 1L, 1L, 1L, 
1L), levels = c("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", 
"Aug", "Sep", "Oct", "Nov", "Dec"), class = "factor"), Date = c("24/01/2022", 
"24/01/2022", "24/01/2022", "24/01/2022", "24/01/2022", "24/01/2022"
), Total.Nitrogen = c(4.987497648, 4.901992077, 3.837696783, 
4.389720566, 3.853376279, 1.696494226), DO = c(9, 9.4, 8.3, 8.5, 
8.8, 8.7), condition = c("Ammonia", "Ammonia", "Ammonia", "Ammonia", 
"Ammonia", "Ammonia"), value = c(0.063, 0.066888889, 0.065333333, 
0.035, 0.052888889, 0.035)), row.names = c(NA, 6L), class = "data.frame")
解决方案

可以使用ggbreak包实现Y轴断裂,既聚焦0-0.5区间的细节,又保留高值数据的展示:

方法1:Y轴断裂展示全量数据

  1. 安装并加载ggbreak包:
install.packages("ggbreak")
library(ggbreak)
  1. 修改原有代码,替换ylim(0,9)为scale_y_break配置断裂规则:
sampleSites <- list("BVR1", "BVR2", "BVR3", "BVR4","BVR5", "BVR6")

samplePlotList <- list()


for (site in sampleSites){
  dat <- subset(nitrogen, ID==site)
  samplePlotList[[site]] <-
    dat %>%
    ggplot(aes_string(x="Month", y="value", group="condition")) +
    geom_line(size=0.5, aes(colour=condition)) +
    theme_classic() +
    theme(legend.position='none') +
    theme(text=element_text(size=10, family='Calibri')) +
    theme(axis.text=element_text(size=10, family='Calibri')) +
    labs(x= NULL) +
    # 设置Y轴在0.5-8区间断裂,前后刻度比例分别为1(0-0.5区间)和0.2(8-9区间)
    scale_y_break(c(0.5, 8), scales = c(1, 0.2)) +
    ylim(0, 9) +
    theme(plot.margin = margin(0.75,0,0.5,0, "cm"))
}

代码说明

  • scale_y_break(c(0.5, 8)):指定Y轴跳过0.5到8的区间,避免该区间拉伸低数值部分的显示比例。
  • scales = c(1, 0.2):让0-0.5区间的刻度更密集,方便观察低浓度氮类型的变化规律;高值区间刻度稀疏,仅保留数据存在的范围。

方法2:仅聚焦0-0.5区间

如果不需要展示高值数据的细节,可直接限制Y轴范围为0-0.5,最大化低浓度曲线的展示空间:

sampleSites <- list("BVR1", "BVR2", "BVR3", "BVR4","BVR5", "BVR6")

samplePlotList <- list()


for (site in sampleSites){
  dat <- subset(nitrogen, ID==site)
  samplePlotList[[site]] <-
    dat %>%
    ggplot(aes_string(x="Month", y="value", group="condition")) +
    geom_line(size=0.5, aes(colour=condition)) +
    theme_classic() +
    theme(legend.position='none') +
    theme(text=element_text(size=10, family='Calibri')) +
    theme(axis.text=element_text(size=10, family='Calibri')) +
    labs(x= NULL) +
    ylim(0, 0.5) +
    theme(plot.margin = margin(0.75,0,0.5,0, "cm"))
}

这种方式简洁直观,适合重点关注低浓度氮类型动态变化的场景,高值数据可单独绘制汇总图表补充展示。

内容的提问来源于stack exchange,提问作者Melanie Baker

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.12 16:01:00