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

Y轴正态分布概率缩放异常:累积频率图Y轴范围不符预期

解决Y轴概率缩放累积频率图的范围问题

你遇到的问题是添加probability_trans("norm")后,Y轴没有显示预期的0-1范围,只展示了0.5-0.95区间。这是因为ggplot2默认会根据数据的实际取值范围自动调整轴的显示区间,而stat_ecdf生成的累积频率值本身是从1/(n+1)到n/(n+1)(n为样本量),如果你的数据累积频率集中在0.5-0.95区间,转换后轴就会自动聚焦这个范围。

原代码

初始正常执行的代码:

plt <- ggplot(df, aes(df["test_values"]))
plt = plt + stat_ecdf()
plt = plt + stat_ecdf(geom="point")
plt = plt + ylab("Cumulative Frequency")
plt = plt + scale_x_continuous(labels = scales::scientific,breaks = scales::pretty_breaks(n = 8))

添加缩放后出现问题的代码:

plt = plt + scale_y_continuous(trans=probability_trans("norm"), breaks=c(0.001,0.01,0.05,0.25,0.5,0.75,0.95,0.99,0.999))

解决方案

在scale_y_continuous中手动指定Y轴范围即可。注意概率转换在0和1处会触发无穷大计算,所以可以用接近0和1的极值避免报错:

plt = plt + scale_y_continuous(
  trans = probability_trans("norm"),
  breaks = c(0.001,0.01,0.05,0.25,0.5,0.75,0.95,0.99,0.999),
  limits = c(1e-6, 1 - 1e-6)
)

若希望直接显示0和1的刻度边界,也可以尝试直接设置limits = c(0, 1),ggplot2会自动适配极值的显示:

plt = plt + scale_y_continuous(
  trans = probability_trans("norm"),
  breaks = c(0.001,0.01,0.05,0.25,0.5,0.75,0.95,0.99,0.999),
  limits = c(0, 1)
)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.08 04:50:41