如何在R中按ID最后change正负生成多幅平均变化趋势图
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_idcolumn groups rows into distinct sequences (each timeamount_IDresets to 1, we start a new sequence). This lets us handle multiple instances of the same ID separately. - Category Assignment: We use
case_whento label each sequence based on its finalchangevalue (positive, negative, or zero). - Cumulative Mean Calculation: We reuse your original logic to compute the cumulative mean of
changefor each sequence, so lines show the average change up to each timespan. - Faceted Plots: Each plot uses
facet_wrapto split data by ID, mirroring your original setup for consistency.
内容的提问来源于stack exchange,提问作者henkjan

