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

如何在R中构建评估X百分位数的绘图?ggplot及x轴设置咨询

在R中用ggplot绘制以百分位数为x轴的比赛时间评估图

嘿,针对你想要用ggplot构建评估X百分位数的绘图(且把百分位数放在x轴)的需求,我推荐绘制分位数函数图(也就是累积分布函数ECDF的逆图),它能直观展示不同百分位数对应的RaceTimes值。下面是具体的实现步骤和代码:

第一步:准备数据并计算分位数

首先我们需要先计算指定百分位数对应的比赛时间值。你可以选择关键的离散百分位数(比如10、25、50、75、90分位),也可以生成0到100的连续百分位数来绘制平滑趋势曲线:

# 先加载ggplot2包
library(ggplot2)

# 假设你的数据框是R5,包含RaceTimes列
# 方法1:计算关键离散百分位数
target_percentiles <- c(10, 25, 50, 75, 90)  # 你可以根据需求调整这些值
quantile_results <- quantile(R5$RaceTimes, probs = target_percentiles / 100)
# 转换为ggplot能用的数据框格式
quantile_df <- data.frame(
  Percentile = target_percentiles,
  RaceTime = as.numeric(quantile_results)
)

# 方法2:生成0-100的连续百分位数(用于绘制平滑曲线)
continuous_percs <- seq(0, 100, by = 1)
continuous_quants <- quantile(R5$RaceTimes, probs = continuous_percs / 100)
continuous_df <- data.frame(
  Percentile = continuous_percs,
  RaceTime = as.numeric(continuous_quants)
)

第二步:用ggplot绘图

方案1:离散百分位数点图(突出关键分位)

如果重点关注少数关键百分位数(比如中位数),可以用点图展示,同时高亮中位数:

ggplot(quantile_df, aes(x = Percentile, y = RaceTime)) +
  geom_point(size = 3, color = "#2c3e50") +
  # 高亮50分位(中位数)
  geom_point(data = subset(quantile_df, Percentile == 50), 
             size = 4, color = "#e74c3c") +
  # 给中位数添加数值标签
  geom_text(data = subset(quantile_df, Percentile == 50),
            aes(label = paste("中位数:", round(RaceTime, 1))),
            vjust = -1.2, color = "#e74c3c") +
  labs(title = "比赛时间关键分位分布",
       x = "百分位数 (%)",
       y = "比赛时间") +
  theme_minimal()

方案2:连续百分位数平滑曲线(展示完整趋势)

如果想看到从0到100分位的完整变化趋势,用连续百分位数绘制曲线,同时标记关键分位:

ggplot(continuous_df, aes(x = Percentile, y = RaceTime)) +
  geom_line(color = "#2c3e50", linewidth = 1) +
  # 叠加关键分位的标记点和数值标签
  geom_point(data = quantile_df, size = 3, color = "#e74c3c") +
  geom_text(data = quantile_df,
            aes(label = round(RaceTime, 1)),
            vjust = -1, color = "#e74c3c") +
  labs(title = "比赛时间完整分位趋势",
       x = "百分位数 (%)",
       y = "比赛时间") +
  theme_minimal()

额外补充:ECDF验证图(可选)

如果你想从另一个角度验证(x轴为比赛时间,y轴为百分位数),可以绘制累积分布函数(ECDF)图,这也是评估百分位数的常用工具:

ggplot(R5, aes(x = RaceTimes)) +
  stat_ecdf(geom = "step", color = "#2c3e50", linewidth = 1) +
  labs(title = "比赛时间累积分布函数(ECDF)",
       x = "比赛时间",
       y = "累积概率 (百分位数 %)") +
  # 把y轴刻度转换成百分比格式
  scale_y_continuous(labels = function(x) paste0(x * 100, "%")) +
  theme_minimal()

这样你就能完全满足需求,清晰看到不同百分位数对应的比赛时间啦!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.06 15:13:12