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

如何在ggplot中绘制带置信区间的累积趋势线?

实现带均值与置信区间的模拟趋势图的替代方法

你已经通过手动分组计算统计量的方式实现了带均值和95%置信区间的模拟趋势图,以下是几种更简洁或高效的替代实现方法:

方法1:使用ggplot2的stat_summary直接计算

无需提前生成汇总数据集,直接在ggplot调用中完成统计量计算与可视化,代码更紧凑:

ggplot(simulations, aes(x = day, y = deaths)) +
  # 绘制所有模拟线
  geom_line(aes(group = sim_id), color = "black", alpha = 0.1) +
  # 计算并绘制均值线
  stat_summary(fun = mean, geom = "line", color = "blue", size = 1.2) +
  # 计算并绘制95%分位数置信区间
  stat_summary(
    fun.min = function(x) quantile(x, 0.025),
    fun.max = function(x) quantile(x, 0.975),
    geom = "ribbon", fill = "blue", alpha = 0.2
  ) +
  labs(
    title = "Simulated Cumulative Deaths with 95% Confidence Interval",
    x = "Day",
    y = "Cumulative Deaths"
  ) +
  scale_y_continuous(
    labels = scales::comma,
    breaks = seq(0, 200000, by = 50000)
  ) +
  theme_minimal() +
  theme(
    panel.grid = element_line(color = "grey90"),
    plot.title = element_text(hjust = 0.5, size = 16)
  )

特点:省去手动生成汇总数据集的步骤,代码逻辑更连贯,适合小到中等规模数据集。

方法2:使用ggdist包的stat_lineribbon

ggdist包专注于分布可视化,stat_lineribbon可一键生成均值线+置信区间带,支持灵活切换统计量与区间参数:
先安装依赖包:

install.packages("ggdist")

绘图代码:

library(ggdist)

ggplot(simulations, aes(x = day, y = deaths)) +
  geom_line(aes(group = sim_id), color = "black", alpha = 0.1) +
  # 自动计算均值和95%分位数区间
  stat_lineribbon(
    fun = "mean",
    .width = 0.95,
    fill = "blue", alpha = 0.2,
    color = "blue", size = 1.2
  ) +
  labs(
    title = "Simulated Cumulative Deaths with 95% Confidence Interval",
    x = "Day",
    y = "Cumulative Deaths"
  ) +
  scale_y_continuous(
    labels = scales::comma,
    breaks = seq(0, 200000, by = 50000)
  ) +
  theme_minimal() +
  theme(
    panel.grid = element_line(color = "grey90"),
    plot.title = element_text(hjust = 0.5, size = 16)
  )

特点:封装度高,可快速切换统计量(如中位数)或区间宽度,适合生成专业的分布可视化图表。

方法3:使用data.table高效计算统计量

如果模拟数据集规模极大(十万级以上行),data.table的分组计算性能远优于dplyr,适合高性能场景:
先安装依赖包:

install.packages("data.table")

代码实现:

library(data.table)

# 转换为data.table格式
setDT(simulations)

# 快速计算每日统计量
summary_stats_dt <- simulations[, .(
  mean_deaths = mean(deaths),
  lower_ci = quantile(deaths, 0.025),
  upper_ci = quantile(deaths, 0.975)
), by = day]

# 绘图逻辑与原代码一致
ggplot() +
  geom_line(data = simulations, 
            aes(x = day, y = deaths, group = sim_id),
            color = "black", alpha = 0.1) +
  geom_ribbon(data = summary_stats_dt,
             aes(x = day, ymin = lower_ci, ymax = upper_ci),
             fill = "blue", alpha = 0.2) +
  geom_line(data = summary_stats_dt,
            aes(x = day, y = mean_deaths),
            color = "blue", size = 1.2) +
  labs(
    title = "Simulated Cumulative Deaths with 95% Confidence Interval",
    x = "Day",
    y = "Cumulative Deaths"
  ) +
  scale_y_continuous(
    labels = scales::comma,
    breaks = seq(0, 200000, by = 50000)
  ) +
  theme_minimal() +
  theme(
    panel.grid = element_line(color = "grey90"),
    plot.title = element_text(hjust = 0.5, size = 16)
  )

特点:处理大数据集时速度优势显著,语法简洁高效。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.15 00:12:43