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

ggplot2:创建锚定Y轴的折线图并添加水平Y轴注释标签

解决方案

针对需求(折线从Y轴边缘起始/从不可见数据点延伸,X轴不显示2010,同时保留Y轴外侧注释标签),提供两种实现方式:

方法一:折线从Y轴直接起始,用annotation_custom添加注释

无需全局关闭裁剪,避免折线超出页面范围,注释直接放在绘图区域外:

library(ggplot2)
library(scales)

data <- data.frame(Decade = c(2010, 2020, 2030, 2040, 2050, 2060),
                   Percent = c(0.15, 0.2, 0.3, 0.5, 0.7, 0.75))

# 筛选出X轴可见范围内的数据
visible_data <- subset(data, Decade >= 2020)

p <- ggplot(visible_data, aes(x = Decade, y = Percent, group = 1)) +
  geom_line(linetype = 2, lwd = 1.5) +
  geom_point() +
  coord_cartesian(ylim = c(0, 1), xlim = c(2020, 2060)) +
  scale_y_continuous(labels = percent) +
  labs(title = "Title", x = "Decade", y = "Percent %") +
  theme(
    panel.grid.major.x = element_blank(),
    panel.grid.major.y = element_line(size = .1, color = "grey"),
    panel.grid.minor.y = element_line(size = .1, color = "grey"),
    panel.background = element_blank(),
    axis.title.x = element_text(size = 15),
    axis.title.y = element_text(size = 15, margin = margin(t = 0, r = 60, b = 0, l = 0)),
    plot.title = element_text(size = 15, face = "bold"),
    # 给左侧注释预留空间
    plot.margin = margin(l = 60, r = 20, t = 20, b = 20)
  )

# 添加Y轴外侧注释
p +
  annotation_custom(
    grob = textGrob("Complete Inclusion", hjust = 1, size = 3.2*ggplot2::.pt, fontface = "italic"),
    xmin = -Inf, xmax = -Inf, ymin = 1.04, ymax = 1.04
  ) +
  annotation_custom(
    grob = textGrob("Complete Exclusion", hjust = 1, size = 3.2*ggplot2::.pt, fontface = "italic"),
    xmin = -Inf, xmax = -Inf, ymin = 0.04, ymax = 0.04
  )

方法二:保留折线从2010延伸的效果,裁剪超出X轴的部分

开启全局裁剪关闭,但只显示X轴范围内的折线部分,同时隐藏2010的点:

library(ggplot2)
library(scales)

data <- data.frame(Decade = c(2010, 2020, 2030, 2040, 2050, 2060),
                   Percent = c(0.15, 0.2, 0.3, 0.5, 0.7, 0.75))

p <- ggplot(data, aes(x = Decade, y = Percent, group = 1)) +
  # 绘制完整折线,由coord_cartesian裁剪掉X轴范围外的部分
  geom_line(linetype = 2, lwd = 1.5) +
  # 只绘制X轴可见范围内的点
  geom_point(data = subset(data, Decade >= 2020)) +
  coord_cartesian(xlim = c(2020, 2060), ylim = c(0, 1), clip = "off") +
  scale_y_continuous(labels = percent) +
  labs(title = "Title", x = "Decade", y = "Percent %") +
  theme(
    panel.grid.major.x = element_blank(),
    panel.grid.major.y = element_line(size = .1, color = "grey"),
    panel.grid.minor.y = element_line(size = .1, color = "grey"),
    panel.background = element_blank(),
    axis.title.x = element_text(size = 15),
    axis.title.y = element_text(size = 15, margin = margin(t = 0, r = 60, b = 0, l = 0)),
    plot.title = element_text(size = 15, face = "bold"),
    # 给左侧注释预留空间
    plot.margin = margin(l = 60, r = 20, t = 20, b = 20)
  )

# 添加Y轴外侧注释
p +
  annotate("text", x = 2010, y = 1.04, label = "Complete Inclusion", hjust = 1.3, size = 3.2, fontface = "italic") +
  annotate("text", x = 2010, y = 0.04, label = "Complete Exclusion", hjust = 1.25, size = 3.2, fontface = "italic")

说明

  • 方法一适合需要折线严格从Y轴边缘开始的场景,注释通过annotation_custom直接定位到绘图区域外,无需修改全局裁剪设置。
  • 方法二适合需要折线从不可见的2010数据点延伸过来的场景,通过coord_cartesian裁剪掉X轴范围外的折线部分,同时关闭裁剪让注释正常显示。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.29 19:24:55