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

使用ylim()时如何保留ggplot折线图的线条显示?

ggplot2分面折线图:限制y轴范围同时保留线条完整性

问题场景

我用以下R代码绘制带点和线的分面折线图:

BPL2.0.5op <- subset(ambiental2021, dependencia=="BPL2.sala" & tipo.control=="NV")
BPL2.0.5op <- BPL2.0.5op[!is.na(BPL2.0.5op[,"part.0.5"]),]
BPL2.0.5op$punto <- naturalsort::naturalfactor(paste("Punto", BPL2.0.5op$punto, sep="."))

ggplot(BPL2.0.5op, 
       aes(x=fecha, y=part.0.5, group = punto, fill = punto)) +
  geom_point(aes(colour = punto), size = 1.5) +
  geom_line(aes(colour = punto), linetype = 1, size = 1) +
  theme(plot.title = element_text(size = 20, face = "bold")) +
  ggtitle("PARTÍCULAS DE 0,5 micras SALA BPL2") +
  ylab("Partículas 0,5 micras/m3") +
  xlab("fecha del control") +
  geom_hline(yintercept = 3520000, colour = "red", size = 1, linetype = 6) +
  scale_x_date(breaks = unique(BPL2.0.5op$fecha), labels = date_format("%d/%m/%Y")) +
  theme(axis.text.x = element_text(angle = 45, hjust = 1, size = 7), legend.position = "none") +
  facet_wrap(~ punto, ncol = 5, strip.position = "left")

但Punto.17的峰值导致其余数据分辨率不足。添加ylim(0,4000000)限制y轴后,超出范围的数据对应的线条消失,图表看起来无数据。希望线条能延伸至y轴上限,即使点超出指定ylim范围,该如何操作?

解决方案

核心原因

ylim()函数会直接过滤掉超出范围的数据点,导致线条因缺失数据而断裂消失。而coord_cartesian()仅调整图表的显示范围,不会删除任何数据,这是解决问题的关键。

方法1:使用coord_cartesian()限制显示范围

替换ylim(0,4000000)为coord_cartesian(ylim = c(0, 4000000)),这样所有数据都会保留,线条完整,只是超出y轴范围的点会被显示在图表区域外。

修改后的完整代码:

BPL2.0.5op <- subset(ambiental2021, dependencia=="BPL2.sala" & tipo.control=="NV")
BPL2.0.5op <- BPL2.0.5op[!is.na(BPL2.0.5op[,"part.0.5"]),]
BPL2.0.5op$punto <- naturalsort::naturalfactor(paste("Punto", BPL2.0.5op$punto, sep="."))

ggplot(BPL2.0.5op, 
       aes(x=fecha, y=part.0.5, group = punto, fill = punto)) +
  geom_point(aes(colour = punto), size = 1.5) +
  geom_line(aes(colour = punto), linetype = 1, size = 1) +
  theme(plot.title = element_text(size = 20, face = "bold")) +
  ggtitle("PARTÍCULAS DE 0,5 micras SALA BPL2") +
  ylab("Partículas 0,5 micras/m3") +
  xlab("fecha del control") +
  geom_hline(yintercept = 3520000, colour = "red", size = 1, linetype = 6) +
  scale_x_date(breaks = unique(BPL2.0.5op$fecha), labels = date_format("%d/%m/%Y")) +
  theme(axis.text.x = element_text(angle = 45, hjust = 1, size = 7), legend.position = "none") +
  facet_wrap(~ punto, ncol = 5, strip.position = "left") +
  # 替换ylim为coord_cartesian
  coord_cartesian(ylim = c(0, 4000000))

方法2:截断超出范围的点到y轴上限

如果希望超出y轴范围的点显示在轴的顶部(而不是隐藏在图表外),可以提前对数据做截断处理,用pmin()将part.0.5的值限制在4000000以内,同时保留原始数据用于线条绘制。

示例代码:

# 新增截断处理
BPL2.0.5op$part.0.5_trunc <- pmin(BPL2.0.5op$part.0.5, 4000000)

ggplot(BPL2.0.5op, 
       aes(x=fecha, y=part.0.5, group = punto, fill = punto)) +
  # 使用截断后的数据绘制点
  geom_point(aes(y=part.0.5_trunc, colour = punto), size = 1.5) +
  geom_line(aes(colour = punto), linetype = 1, size = 1) +
  theme(plot.title = element_text(size = 20, face = "bold")) +
  ggtitle("PARTÍCULAS DE 0,5 micras SALA BPL2") +
  ylab("Partículas 0,5 micras/m3") +
  xlab("fecha del control") +
  geom_hline(yintercept = 3520000, colour = "red", size = 1, linetype = 6) +
  scale_x_date(breaks = unique(BPL2.0.5op$fecha), labels = date_format("%d/%m/%Y")) +
  theme(axis.text.x = element_text(angle = 45, hjust = 1, size = 7), legend.position = "none") +
  facet_wrap(~ punto, ncol = 5, strip.position = "left") +
  coord_cartesian(ylim = c(0, 4000000))

这样处理后,线条会完整保留,超出y轴上限的点会显示在轴的顶部,既保证了其余数据的分辨率,又不会丢失线条的连续性。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.04 03:01:04