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

如何在R中设置碎石图主次Y轴范围为0-30和0-100?

问题描述

拥有如下数据集:

structure(list(X = c(26.7788998092606, 18.6280163936587, 14.349845629237, 
9.66310505252417, 7.94423462318618, 5.04943860391983, 3.27216212415563, 
2.94046432374716, 2.16429582651011, 1.63761158654806, 1.11240348866093
), X0 = 1:11, index1 = c(26.7788998092606, 18.6280163936587, 
14.349845629237, 9.66310505252417, 7.94423462318618, 5.04943860391983, 
3.27216212415563, 2.94046432374716, 2.16429582651011, 1.63761158654806, 
1.11240348866093), variance_pct = c(28.6281410315749, 19.9143909665674, 
15.3407872385057, 10.3303995390774, 8.49283095274311, 5.39813216797301, 
3.49812424840959, 3.14352075545081, 2.31375323843416, 1.75069834043094, 
1.18922152083292), cumulative_variance = c(28.6281410315749, 
48.5425319981423, 63.883319236648, 74.2137187757255, 82.7065497284686, 
88.1046818964416, 91.6028061448512, 94.746326900302, 97.0600801387361, 
98.8107784791671, 100)), row.names = c(NA, -11L), class = "data.frame")

需要创建碎石图,要求:

  • 主Y轴范围为0-30,对应单因子方差解释率(variance_pct)
  • 次Y轴范围为0-100,对应累积方差解释率(cumulative_variance)

当前代码如下:

p2 <- ggplot(data = pca, aes(x = X0)) +
  geom_point(aes(y = variance_pct), size = 4, color = "blue") +
  geom_line(aes(y = variance_pct), color = "blue") +
  scale_y_continuous(name = "Variance Explained (%)", breaks = seq(0, 30, 5)) +
  labs(title = "Scree Plot") +
  theme_bw() +
  scale_y_continuous(
    sec.axis = sec_axis(~ . * 100 / 30, name = "Cumulative Variance Explained (%)", breaks = seq(0, 100, 20)),
    limits = c(0, 100)
  ) +
  geom_point(aes(y = cumulative_variance), size = 4, color = "red") +
  geom_line(aes(x = X0, y = cumulative_variance), color = "red") +
  labs(title = "Scree Plot with Cumulative Variance Explained")

p2

生成的图表不符合Y轴范围要求,需要修正代码。

问题分析

当前代码的核心问题:

  1. 重复调用scale_y_continuous(),后一次调用会覆盖前一次的设置,导致主Y轴范围被改成了0-100,不符合需求
  2. 累积方差的数值(0-100)没有映射到主Y轴的刻度区间(0-30),直接绘制会超出主Y轴的预期范围
解决方案

修正思路:

  • 只保留一个scale_y_continuous(),设置主Y轴的范围为0-30
  • 将累积方差数据按比例转换为0-30区间的数值(即cumulative_variance * 30 / 100),使其与主Y轴刻度匹配
  • 次Y轴通过逆转换(~ . * 100 / 30)将主Y轴的刻度值转换为0-100的累积方差刻度

修正后的代码:

p2 <- ggplot(data = pca, aes(x = X0)) +
  # 绘制单因子方差解释率(主Y轴)
  geom_point(aes(y = variance_pct), size = 4, color = "blue") +
  geom_line(aes(y = variance_pct), color = "blue") +
  # 绘制转换后的累积方差解释率(匹配主Y轴刻度)
  geom_point(aes(y = cumulative_variance * 30 / 100), size = 4, color = "red") +
  geom_line(aes(y = cumulative_variance * 30 / 100), color = "red") +
  # 设置Y轴:主轴0-30,次轴0-100
  scale_y_continuous(
    name = "Variance Explained (%)",
    breaks = seq(0, 30, 5),
    limits = c(0, 30),
    sec.axis = sec_axis(
      ~ . * 100 / 30,
      name = "Cumulative Variance Explained (%)",
      breaks = seq(0, 100, 20)
    )
  ) +
  labs(title = "Scree Plot with Cumulative Variance Explained") +
  theme_bw()

p2
效果说明
  • 主Y轴显示单因子方差解释率,范围严格控制在0-30,刻度间隔为5
  • 次Y轴显示累积方差解释率,范围0-100,刻度间隔为20
  • 红色的累积方差曲线会随着主Y轴刻度对应到次Y轴的正确数值,不会出现刻度混乱的问题

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.27 03:22:05