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

如何用ggplot2 geom_lines按不同时间段均值绘制多色折线图

基于不同时间窗口均值绘制多色折线图

原始数据集

df <- structure(list(date_time = structure(c(1577836800, 1577840400, 
1577844000, 1577847600, 1577851200, 1577854800, 1577858400, 1577862000, 
1577865600, 1577869200, 1577872800, 1577876400, 1577880000, 1577883600, 
1577887200, 1577890800, 1577894400, 1577898000, 1577901600, 1577905200, 
1577908800, 1577912400, 1577916000, 1577919600, 1577923200, 1577926800, 
1577930400, 1577934000, 1577937600, 1577941200, 1577944800, 1577948400, 
1577952000, 1577955600, 1577959200, 1577962800, 1577966400, 1577970000, 
1577973600, 1577977200, 1577980800, 1577984400, 1577988000, 1577991600, 
1577995200, 1577998800, 1578002400, 1578006000, 1578009600, 1578013200, 
1578016800, 1578020400, 1578024000, 1578027600, 1578031200, 1578034800, 
1578038400, 1578042000, 1578045600, 1578049200, 1578052800, 1578056400, 
1578060000, 1578063600, 1578067200, 1578070800, 1578074400, 1578078000, 
1578081600, 1578085200, 1578088800, 1578092400, 1578096000, 1578099600, 
1578103200, 1578106800, 1578110400, 1578114000, 1578117600, 1578121200, 
1578124800, 1578128400, 1578132000, 1578135600, 1578139200, 1578142800, 
1578146400, 1578150000, 1578153600, 1578157200, 1578160800, 1578164400, 
1578168000, 1578171600, 1578175200, 1578178800, 1578182400, 1578186000, 
1578189600, 1578193200), tzone = "UTC", class = c("POSIXct", 
"POSIXt")), altura = c(80L, 67L, 69L, 86L, 110L, 137L, 161L, 
178L, 182L, 174L, 153L, 126L, 102L, 86L, 84L, 94L, 113L, 137L, 
161L, 180L, 189L, 183L, 164L, 136L, 107L, 86L, 77L, 81L, 95L, 
115L, 137L, 157L, 171L, 175L, 167L, 149L, 127L, 107L, 94L, 91L, 
99L, 114L, 134L, 156L, 173L, 181L, 177L, 160L, 136L, 112L, 94L, 
85L, 87L, 98L, 114L, 133L, 152L, 166L, 172L, 167L, 152L, 133L, 
113L, 99L, 93L, 97L, 109L, 128L, 150L, 168L, 178L, 175L, 161L, 
140L, 117L, 99L, 88L, 87L, 94L, 109L, 129L, 151L, 168L, 176L, 
173L, 159L, 138L, 116L, 98L, 88L, 89L, 102L, 123L, 147L, 168L, 
180L, 180L, 166L, 145L, 120L)), row.names = c(NA, -100L), class = c("tbl_df", 
"tbl", "data.frame"))

需求说明

现有单条随时间变化的altura数值折线图,需生成样式相似的折线图,但分别按6小时、12小时、24小时的时间窗口计算均值,不同窗口对应的折线用不同颜色区分。

实现步骤

1. 加载依赖包

library(tidyverse)
library(lubridate)

2. 按时间窗口聚合数据

将原始数据按不同时间窗口分组,计算每组的altura均值,并整理为适合绘图的长格式:

df_agg <- bind_rows(
  # 6小时窗口均值
  df %>%
    mutate(window = "6小时") %>%
    group_by(window, date_time = floor_date(date_time, "6 hours")) %>%
    summarise(altura_mean = mean(altura), .groups = "drop"),
  # 12小时窗口均值
  df %>%
    mutate(window = "12小时") %>%
    group_by(window, date_time = floor_date(date_time, "12 hours")) %>%
    summarise(altura_mean = mean(altura), .groups = "drop"),
  # 24小时窗口均值
  df %>%
    mutate(window = "24小时") %>%
    group_by(window, date_time = floor_date(date_time, "24 hours")) %>%
    summarise(altura_mean = mean(altura), .groups = "drop")
)

3. 绘制多色折线图

使用ggplot2生成匹配原始样式的折线图,用颜色区分不同时间窗口:

ggplot(df_agg, aes(x = date_time, y = altura_mean, color = window)) +
  geom_line(linewidth = 1) + # 调整线条粗细匹配原始图
  labs(
    x = "时间",
    y = "平均高度",
    title = "不同时间窗口的高度均值变化",
    color = "时间窗口"
  ) +
  theme_minimal() +
  scale_color_brewer(palette = "Set1") # 用预设调色板区分窗口

额外调整(可选)

如果需要用线条样式(实线/虚线)进一步区分窗口,可修改绘图代码:

ggplot(df_agg, aes(x = date_time, y = altura_mean, color = window, linetype = window)) +
  geom_line(linewidth = 1) +
  labs(
    x = "时间",
    y = "平均高度",
    title = "不同时间窗口的高度均值变化",
    color = "时间窗口",
    linetype = "时间窗口"
  ) +
  theme_minimal() +
  scale_color_brewer(palette = "Set1") +
  scale_linetype_manual(values = c("solid", "dashed", "dotted"))

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.13 09:15:35