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

ggplot2绘制多折线图报错:每组仅一个观测值,求解决方案

Fixing the ggplot Line Chart Error for Your 7-Day Cumulative Count Data

Hey there! Let's get that line chart working properly. The error you're seeing (geom_path: Each group consists of only one observation) happens because two key issues are throwing off ggplot's ability to draw lines:

  • Your data is in wide format (each day is its own column), which confuses ggplot about how to group points into continuous lines.
  • Using reorder() on the time intervals breaks the natural grouping needed to connect sequential points for each day.

Step 1: Understand the Root Cause

When you use reorder(value, MON) in your aesthetic mapping, ggplot treats each reordered time interval as a separate group. Since each group only has one data point (one day's value at that interval), it can't draw a line between points. We need to restructure your data and adjust the ggplot logic to fix this.

Step 2: Restructure Your Data to Long Format

First, let's rename the time column for clarity, then convert your wide data into long format (each row will represent one time interval + one day's cumulative count). This makes it easy for ggplot to identify which points belong to each day:

library(tidyverse)

# Keep your existing code to generate mydf, then:
mydf <- mydf %>%
  rename(time_interval = value) # Rename the time column to avoid confusion

# Convert wide data to long format
long_df <- mydf %>%
  pivot_longer(
    cols = -time_interval, # Keep the time column as-is
    names_to = "day",      # New column to store day names
    values_to = "cumulative_count" # New column for cumulative count values
  )

Step 3: Build the Corrected Line Chart

Now we can plot the data correctly. We'll:

  • Use your original time interval order (no need for reorder() since you already generated them in the critical midnight-to-midnight sequence)
  • Group by day so ggplot connects points for each day into a continuous line
  • Use light, subtle colors for individual days and a bold, eye-catching color for the average
ggplot(long_df, aes(x = time_interval, y = cumulative_count, group = day, color = day)) +
  geom_line(linewidth = 1) +
  # Custom color scale: light grays for daily counts, bold orange for the average
  scale_color_manual(values = c(
    MON = "#f0f0f0", TUE = "#e0e0e0", WED = "#d0d0d0",
    THUR = "#c0c0c0", FRI = "#b0b0b0", SAT = "#a0a0a0",
    SUN = "#909090", AVG = "#ff6b35"
  )) +
  # Add clear labels and adjust x-axis text to avoid overlap
  labs(
    x = "15-Minute Time Interval",
    y = "Cumulative Count",
    title = "7-Day Cumulative Counts & Daily Average"
  ) +
  theme(
    axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1),
    plot.title = element_text(hjust = 0.5)
  )

Why This Works

  • Long format data explicitly tells ggplot which points belong to each day, so it can draw continuous lines for each group.
  • We preserve your original time_interval order, keeping the critical sequence from midnight to midnight intact.
  • The color scale ensures individual daily trends are subtle (light grays) while the average stands out with a bold, warm color that draws attention.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 06:29:46