能否在ggplot2的geom_point中为点添加图案填充以展示额外变量?
Absolutely! While base ggplot2 doesn’t natively support patterned fills for points (the kind you see in Excel with stripes, hashes, etc.), you can easily pull this off with the ggpattern package. It’s purpose-built for adding fill patterns to ggplot2 geoms, and works perfectly for your use case—pairing color (for risk value) with a patterned fill (for your extra variable) is way more visually intuitive than just swapping point shapes.
Here’s a step-by-step guide to implement this:
1. Install and Load Required Packages
First, install ggpattern if you haven’t already, then load it alongside ggplot2:
install.packages("ggpattern") library(ggpattern) library(ggplot2)
2. Use geom_point_pattern() Instead of geom_point()
This function extends geom_point() to support pattern aesthetics. The key is to use a point shape that allows independent fill and border colors (shapes 21-25 work best—these are the "filled" shapes with a separate outline).
Example Code with Simulated Data
Let’s use a sample dataset that mirrors your scenario (multiple people, risk values over time, plus an extra categorical variable):
# Simulate sample data matching your use case set.seed(123) df <- data.frame( person = rep(paste0("Person_", 1:5), each = 4), time = rep(1:4, 5), risk_value = runif(20, 0, 10), # Random risk values extra_variable = sample(c("High_Priority", "Standard"), 20, replace = TRUE) ) # Create the plot with color (risk) and pattern (extra variable) ggplot(df, aes(x = time, y = person)) + geom_point_pattern( # Map aesthetics: color = risk, pattern = extra variable aes(fill = risk_value, pattern = extra_variable), shape = 21, # Critical: use a fillable shape (21-25) color = "black", # Point border color size = 5, # Adjust point size for visibility # Pattern customization (match Excel-style patterns) pattern_fill = "white", # Color of the pattern itself pattern_color = "black", # Outline color of the pattern pattern_density = 0.3, # How dense the pattern is (0 = none, 1 = solid) pattern_spacing = 0.05, # Spacing between pattern elements pattern_type = c("stripe", "crosshatch") # Explicitly set patterns for your variable ) + # Add scales for clarity scale_fill_viridis_c(name = "Risk Value") + scale_pattern_discrete(name = "Priority Level") + # Labels and theme labs(x = "Time Period", y = "Participant", title = "Risk Value Over Time with Priority Pattern") + theme_minimal()
Key Customization Tips
- Pattern Types: You can specify exact patterns using
pattern_type—options include"stripe","crosshatch","circle","dot", and more, which match Excel’s fill patterns closely. - Pattern Density/Spacing: Tweak
pattern_densityandpattern_spacingto make patterns more or less prominent, depending on your plot’s readability. - Shape Compatibility: Only shapes 21-25 support fill patterns, so stick to those to ensure the patterns render correctly.
This approach keeps your color scale dedicated to risk values (so readers can easily interpret that variable) while using patterns to clearly distinguish your extra variable—no confusion with overlapping shape meanings, and far more intuitive than relying solely on point shapes.
内容的提问来源于stack exchange,提问作者Ant

