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

ggplot2绘图值与数据集不符问题求助

问题解决:ggplot2绘制行为时间轴与数据不符

问题根源

  • 时间变量处理逻辑错误:将Start/End转为POSIXlt后又转回字符串,ggplot会按字符串字典序而非实际时间顺序排序,导致时间轴展示混乱。
  • 跨天时间段未适配:像21:00到00:00这类跨午夜的记录,直接用纯时间字符串无法在单日面板中正确映射。
  • 冗余排序干扰:arrange(Behaviour)打乱数据关联,虽不直接引发错误,但属于不必要的操作。

修正步骤

1. 正确整合日期与时间

将日期和时间字段合并为完整时间戳,同时处理跨天场景:

library(tidyverse)
library(lubridate)

# 加载数据(注意原代码中赋值符号需替换为英文<-)
gar <- structure(list(Day = c("22-02-2023", "22-02-2023", "22-02-2023", 
"23-02-2023", "23-02-2023", "23-02-2023", "24-02-2023", "24-02-2023", 
"24-02-2023", "24-02-2023", "27-02-2023", "27-02-2023", "28-02-2023", 
"28-02-2023", "28-02-2023", "28-02-2023", "28-02-2023", "28-02-2023", 
"22-02-2023", "22-02-2023", "22-02-2023", "23-02-2023", "23-02-2023", 
"24-02-2023", "24-02-2023", "24-02-2023", "27-02-2023", "27-02-2023", 
"28-02-2023", "28-02-2023", "28-02-2023", "22-02-2023", "22-02-2023", 
"22-02-2023", "22-02-2023", "23-02-2023", "23-02-2023", "24-02-2023", 
"24-02-2023", "24-02-2023", "27-02-2023", "27-02-2023", "28-02-2023", 
"28-02-2023", "28-02-2023", "28-02-2023", "28-02-2023", "22-02-2023", 
"23-02-2023", "24-02-2023", "24-02-2023", "27-02-2023", "28-02-2023"
), Behaviour = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("Red", 
"Amber", "Green", "Asleep"), class = "factor"), Start = c("12:00", 
"17:00", "21:00", "09:00", "18:00", "21:00", "09:00", "14:00", 
"18:00", "20:00", "10:00", "18:00", "12:00", "17:00", "19:00", 
"00:00", "02:00", "04:00", "09:00", "15:00", "00:00", "06:00", 
"22:00", "06:00", "16:00", "22:00", "06:00", "21:00", "06:00", 
"15:00", "22:00", "07:00", "11:00", "16:00", "20:00", "08:00", 
"20:00", "07:00", "15:00", "17:00", "09:00", "23:00", "07:00", 
"11:00", "16:00", "18:00", "20:00", "13:00", "17:00", "11:00", 
"19:00", "16:00", "08:00"), End = c("15:00", "20:00", "00:00", 
"17:00", "20:00", "22:00", "11:00", "15:00", "19:00", "22:00", 
"16:00", "21:00", "15:00", "18:00", "20:00", "01:00", "03:00", 
"05:00", "11:00", "16:00", "06:00", "08:00", "06:00", "07:00", 
"17:00", "06:00", "09:00", "06:00", "07:00", "16:00", "06:00", 
"09:00", "12:00", "17:00", "21:00", "09:00", "21:00", "09:00", 
"16:00", "18:00", "10:00", "00:00", "08:00", "12:00", "17:00", 
"19:00", "22:00", "14:00", "19:00", "14:00", "20:00", "18:00", 
"11:00")), row.names = c(NA, -53L), class = c("tbl_df", "tbl", 
"data.frame"))

# 处理日期时间,适配跨天场景
gar <- gar %>%
  mutate(
    Day = dmy(Day),
    # 合并日期与开始时间
    Start_datetime = ymd_hm(paste(Day, Start)),
    # 结束时间为00:00时,自动加一天
    End_datetime = if_else(End == "00:00",
                          ymd_hm(paste(Day + days(1), End)),
                          ymd_hm(paste(Day, End))),
    # 提取当日时间(用于x轴展示)
    Start_time = hms(Start),
    End_time = if_else(End == "00:00", hms("24:00"), hms(End)),
    # 定义Behaviour因子顺序
    Behaviour = factor(Behaviour, levels = c("Red", "Amber", "Green", "Asleep"))
  )

2. 调整绘图代码,使用连续时间轴

让x轴按时间顺序排列,而非字符串顺序:

ggplot(gar, aes(colour = Behaviour)) +
  geom_segment(aes(x = Start_time, xend = End_time, y = Behaviour, yend = Behaviour), size = 6) +
  facet_wrap(~Day, ncol = 1) +
  # 设置x轴为时间格式,每2小时显示刻度
  scale_x_time(labels = scales::time_format("%H:%M"), breaks = scales::time_breaks("2 hours")) +
  scale_colour_manual(values = c(Red = "red", Green = "#99CC33", Amber = "#FFCC00", Asleep = "gray")) +
  theme_minimal() +
  labs(x = "Duration", y = "Behaviour")

关键说明

  • 用dmy()和ymd_hm()正确解析日期时间,彻底避免字符串排序错误。
  • 跨天的00:00结束时间处理为24:00,保证时间段在单日面板中完整展示。
  • scale_x_time()强制x轴按实际时间顺序渲染,匹配数据逻辑。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.28 00:17:03