如何在R语言中使用ggplot2将多个绘图合并为单个图形?
Hey there! Looks like you want to combine those separate ggplot smooth plots into a single figure for easier comparison—great call, that’s way better than flipping between plots. Here are two solid approaches to do this, depending on your workflow:
This is the cleanest approach because it leverages ggplot’s built-in grouping and legend handling. The idea is to add an identifier column to each dataset, then merge them into one data frame.
First, add a column to label each merchandise group:
# Add a merch identifier to each dataset merch42$merch_label <- "Merch 42" merch323$merch_label <- "Merch 323" merch24$merch_label <- "Merch 24" # Assuming this follows the same structure as the others
Next, combine all datasets into one using dplyr::bind_rows() (install the dplyr package if you don’t have it already):
library(dplyr) combined_data <- bind_rows(merch42, merch323, merch24)
Now you can plot all trends in one go, with colors mapped to the merch label (and a proper legend automatically generated):
library(ggplot2) ggplot(combined_data, aes(x = day_code, y = avg_logistic_review_score, color = merch_label)) + geom_smooth(method = "loess", span = 1/25) + # Customize colors to match your original plots (optional but consistent) scale_color_manual(values = c("Merch 42" = "red", "Merch 323" = "blue", "Merch 24" = "darkgreen")) + # Add clear labels for readability labs( title = "Average Logistic Review Score Trend by Merchandise", x = "Day Code", y = "Average Logistic Review Score", color = "Merchandise" ) + theme_minimal() # Or your preferred theme
If you don’t want to merge your datasets for some reason, you can add each geom_smooth() layer to the same base ggplot object. Just make sure to specify the data argument for each layer:
library(ggplot2) # Start with an empty base plot (or use one dataset as the base—doesn't matter) combined_plot <- ggplot() + # Add Merch 42 trend geom_smooth( data = merch42, aes(x = day_code, y = avg_logistic_review_score), method = "loess", span = 1/25, color = "red" ) + # Add Merch 323 trend geom_smooth( data = merch323, aes(x = day_code, y = avg_logistic_review_score), method = "loess", span = 1/25, color = "blue" ) + # Add Merch 24 trend geom_smooth( data = merch24, aes(x = day_code, y = avg_logistic_review_score), method = "loess", span = 1/25, color = "darkgreen" ) + # Add labels and a manual legend (since we're hardcoding colors) labs( title = "Average Logistic Review Score Trend by Merchandise", x = "Day Code", y = "Average Logistic Review Score" ) + scale_color_identity( name = "Merchandise", breaks = c("red", "blue", "darkgreen"), labels = c("Merch 42", "Merch 323", "Merch 24"), guide = "legend" ) + theme_minimal()
A quick note: Method 1 is almost always better long-term—it’s easier to update, add more merch groups later, and ggplot handles legends/consistency for you. Just make sure all your datasets have matching column names (day_code and avg_logistic_review_score) for either method to work smoothly.
内容的提问来源于stack exchange,提问作者Phil R

