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

如何在R中按ID最后change正负生成多幅平均变化趋势图

Solution to Generate Plots by Final Change Sign

First, we need to handle the fact that the same ID appears in multiple separate sequences (like ID3 has two distinct groups of rows with different timespan values). We'll first label each of these sequences, then categorize them based on their final change value, and finally create the three plots you need.

Step 1: Prepare the Data

We'll add a unique identifier for each sequence and categorize each sequence by its final change value:

library(ggplot2)
library(dplyr)

# Assuming your data frame is named 'df'
df_processed <- df %>%
  # Ensure data is ordered correctly to group sequences properly
  arrange(ID, timespan) %>%
  # Create a unique ID for each separate sequence (triggers when amount_ID resets to 1)
  mutate(sequence_id = cumsum(amount_ID == 1)) %>%
  # For each sequence, get the final change value and assign a category
  group_by(sequence_id) %>%
  mutate(
    final_change = last(change),
    change_category = case_when(
      final_change > 0 ~ "Positive Ending",
      final_change < 0 ~ "Negative Ending",
      TRUE ~ "Zero Ending"
    )
  ) %>%
  ungroup() %>%
  # Calculate cumulative mean change for each sequence (matches your original logic)
  group_by(sequence_id) %>%
  mutate(cumulative_change = cummean(change)) %>%
  ungroup()

Step 2: Generate the Three Plots

We'll create separate faceted plots (like your original code) for each category to keep each ID's trends distinct.

Plot 1: Sequences with Positive Final Change

This includes both sequences for ID3 and one sequence for ID9:

plot_positive <- df_processed %>%
  filter(change_category == "Positive Ending") %>%
  ggplot(aes(x = timespan, y = cumulative_change)) +
  geom_line(linewidth = 1) +
  facet_wrap(~ID, scales = "free_y") +
  labs(
    title = "Cumulative Mean Change (Positive Final Change)",
    x = "Timespan",
    y = "Cumulative Mean Change"
  ) +
  theme_minimal()

print(plot_positive)

Plot 2: Sequences with Negative Final Change

This includes one sequence for ID9:

plot_negative <- df_processed %>%
  filter(change_category == "Negative Ending") %>%
  ggplot(aes(x = timespan, y = cumulative_change)) +
  geom_line(linewidth = 1) +
  facet_wrap(~ID, scales = "free_y") +
  labs(
    title = "Cumulative Mean Change (Negative Final Change)",
    x = "Timespan",
    y = "Cumulative Mean Change"
  ) +
  theme_minimal()

print(plot_negative)

Plot 3: Sequences with Zero Final Change

This includes the sequence for ID9 where the final change is 0:

plot_zero <- df_processed %>%
  filter(change_category == "Zero Ending") %>%
  ggplot(aes(x = timespan, y = cumulative_change)) +
  geom_line(linewidth = 1) +
  facet_wrap(~ID, scales = "free_y") +
  labs(
    title = "Cumulative Mean Change (Zero Final Change)",
    x = "Timespan",
    y = "Cumulative Mean Change"
  ) +
  theme_minimal()

print(plot_zero)

Key Details

  • Sequence Identification: The sequence_id column groups rows into distinct sequences (each time amount_ID resets to 1, we start a new sequence). This lets us handle multiple instances of the same ID separately.
  • Category Assignment: We use case_when to label each sequence based on its final change value (positive, negative, or zero).
  • Cumulative Mean Calculation: We reuse your original logic to compute the cumulative mean of change for each sequence, so lines show the average change up to each timespan.
  • Faceted Plots: Each plot uses facet_wrap to split data by ID, mirroring your original setup for consistency.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.07 11:27:27