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

如何用R的ggplot轻松格式化100+条试验曲线?

用ggplot绘制单试验灰色曲线+突出均值曲线的实现

实现思路

  • 先绘制所有单个试验的曲线,统一设为浅灰色并降低透明度,弱化个体以体现离散分布
  • 单独计算每个时间点的均值,绘制一条高优先级的均值曲线作为核心参考

修改后的完整代码

library(tidyverse)

# 可复现数据
dat = structure(list(Trial = c("x1", "x2", "x3", "x4", "x5", "x6"), 
                     `1` = c(69824.35, 69824.35, 69824.35, 69824.35, 69824.35, 
                             69824.35), `2` = c(67628.96, 67628.96, 67628.96, 67628.96, 
                                                67628.96, 67628.96), `3` = c(67976.71, 67991.52, 67973.59, 
                                                                             67939.18, 67983.88, 67955.75), `4` = c(68263.39, 68290.91, 
                                                                                                                    68258.04, 68205.13, 68278.1, 68231.44), `5` = c(68500.94, 
                                                                                                                                                                    68538.68, 68494.1, 68433.12, 68522.57, 68464.4), `6` = c(68698.74, 
                                                                                                                                                                                                                             68744.19, 68691, 68628.56, 68726.19, 68661.55), `7` = c(68864.2, 
                                                                                                                                                                                                                                                                                     68915.02, 68856.02, 68796.14, 68896.15, 68828.66), `8` = c(69003.2, 
                                                                                                                                                                                                                                                                                                                                                69057.35, 68994.91, 68939.84, 69038.34, 68970.55), `9` = c(69120.43, 
                                                                                                                                                                                                                                                                                                                                                                                                           69176.19, 69112.27, 69063.11, 69157.55, 69091.22)), row.names = c(NA, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                             -6L), class = c("tbl_df", "tbl", "data.frame"))

# 转换为长格式并提前处理时间列
transform_dat = dat |>
  pivot_longer(-Trial, names_to = "Time", values_to = "Value") |>
  mutate(Time = as.numeric(Time))

# 计算每个时间点的均值
mean_dat = transform_dat |>
  group_by(Time) |>
  summarise(Mean_Value = mean(Value), .groups = "drop")

# 绘制图表
ggplot() +
  # 绘制单试验曲线:统一浅灰色+低透明度,避免图例冗余
  geom_line(data = transform_dat, 
            aes(x = Time, y = Value), 
            colour = "#999999", alpha = 0.6) +
  # 叠加均值曲线:高饱和颜色+加粗,突出核心趋势
  geom_line(data = mean_dat, 
            aes(x = Time, y = Mean_Value), 
            colour = "#E63946", size = 1.2) +
  # 自定义标签与主题
  labs(x = "时间点", y = "数值", title = "试验曲线分布与均值趋势") +
  theme_minimal()

关键细节说明

  • 单试验曲线使用固定灰色,不映射Trial变量,避免生成大量无效图例
  • alpha参数降低单曲线透明度,曲线重叠时能更直观体现离散密度
  • 均值曲线采用高饱和度颜色+加粗设置,确保视觉优先级
  • 提前转换时间列为数值型,简化后续代码逻辑

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.27 21:37:45