平滑分组比例图技术问询:基于给定R数据集的绘图实现
Hey there! Let's work through creating that smooth grouped proportion plot with your dataset. First, I noticed a tiny issue in your original dataset code (the group vector length didn't match the rest of the data), so I'll fix that first, then walk you through two straightforward approaches to build the plot using ggplot2—the go-to tool for this kind of visualization in R.
Step 1: Fix & Prepare the Dataset
Your original Data creation code would throw an error because rep(c("A","B"),25) only generates 50 values, but the other columns have 10,000 rows. Here's the corrected version to make the dataset valid:
library(dplyr) library(ggplot2) set.seed(10) start_date <- as.Date('2000-01-01') end_date <- as.Date('2000-01-10') Data <- data.frame( id = rep((1:1000), 10), group = rep(c("A","B"), length.out = 10000), # Match length to 10k rows x = sample(1:100, 10000, replace = TRUE), # Add replace=TRUE to avoid length mismatches y = sample(c("1", "0"), 10000, replace = TRUE), date = as.Date( sample(as.numeric(start_date):as.numeric(end_date), 10000, replace = T), origin = '2000-01-01' ) )
Step 2: Smooth Grouped Proportion Plot (Two Approaches)
We want to visualize the proportion of y=1 for each group across dates, with a smooth trend line. Here are two practical ways to do this:
Approach 1: Direct Smoothing with stat_smooth
This method lets ggplot2 handle calculating the proportion and fitting the smooth curve in one streamlined step:
Data %>% mutate( treated = factor(group), date = as.POSIXct(date), y_num = as.numeric(y) # Convert "1"/"0" to numeric to calculate proportions ) %>% ggplot(aes(x = date, y = y_num, color = treated)) + stat_smooth( method = "loess", # Use local regression for a natural smooth curve se = TRUE, # Show confidence intervals around the trend formula = y ~ x, span = 0.7 # Adjust smoothness (higher = more smoothed) ) + labs( title = "Smoothed Proportion of y=1 by Group & Date", x = "Date", y = "Proportion of y=1", color = "Group" ) + theme_minimal()
Approach 2: Aggregate First, Then Smooth
If you want to explicitly calculate daily proportions for each group before adding the smooth line (great for seeing raw data points alongside the trend):
# First calculate daily proportions for each group Data_summary <- Data %>% mutate(treated = factor(group), date = as.Date(date)) %>% group_by(treated, date) %>% summarise( prop_y1 = mean(as.numeric(y)), # Calculate proportion of y=1 for the group/date .groups = "drop" ) # Plot raw proportion points + smooth trend line ggplot(Data_summary, aes(x = date, y = prop_y1, color = treated)) + geom_point(size = 2, alpha = 0.7) + # Show actual daily proportion values geom_smooth(method = "loess", se = TRUE, span = 0.7) + labs( title = "Smoothed Proportion of y=1 by Group & Date", x = "Date", y = "Proportion of y=1", color = "Group" ) + theme_minimal()
Quick Customization Tips
- Swap
method = "loess"formethod = "gam"if you want a more flexible generalized additive model fit - Tweak the
spanvalue (try 0.5 for less smooth, 0.9 for more smooth) to match your data's trend - Change the color palette with
scale_color_brewer(palette = "Set2")for a more polished look
内容的提问来源于stack exchange,提问作者Ivo

