求助:ggplot2堆叠条形图颜色修改无效问题
Hey there! Let's troubleshoot why your color tweaks for the stacked bar plot in ggplot2 aren't working. First, let's break down the common pitfalls you might have hit, then walk through working solutions with your dataset.
First, let's make sure we're working with the same data:
IC <- structure(list(station = c(1L, 1L, 2L, 2L, 3L, 3L, 4L, 4L, 5L, 5L, 6L, 6L, 7L, 7L, 8L, 8L, 9L, 9L, 10L, 10L), morning = c(11L, 22L, 11L, 22L, 11L, 22L, 11L, 22L, 11L, 22L, 11L, 22L, 11L, 22L, 11L, 22L, 11L, 22L, 11L, 22L), number = c(52L, 48L, 38L, 48L, 68L, 32L, 144L, 63L, 125L, 40L, 37L, 9L, 3L, 0L, 18L, 6L, 13L, 35L, 39L, 60L)), class = "data.frame", row.names = c(NA, -20L ))
What was going wrong with your attempts?
- Missing
fillmapping: The biggest issue is likely that you didn't explicitly map themorningcolumn to thefillaesthetic in your plot. Without this, ggplot has no idea which groups to color differently. - Confusing
colorvsfill:scale_color_*controls the color of lines/borders, while stacked bars usescale_fill_*for their interior color. Yourscale_color_gradientcall was targeting the wrong aesthetic. - Incorrect gradient parameters:
scale_color_gradient/scale_fill_gradientuseslowandhighto define gradient endpoints, not your group values like11or22. - Treating discrete values as continuous: Your
morningcolumn is a discrete grouping variable (only 11 and 22), so it works better as a factor rather than a numeric value in ggplot.
Working Solutions
1. Manual fill colors (your original scale_fill_manual approach, fixed)
This is the best fit for your discrete groups:
library(ggplot2) ggplot(IC, aes(x = factor(station), y = number, fill = factor(morning))) + geom_col(position = "stack") + # geom_col is for pre-summarized data, perfect here scale_fill_manual( values = c('#999999','#E69F00'), # Your desired colors labels = c("Group 11", "Group 22"), # Optional: Make legend labels clearer name = "Morning Group" # Optional: Rename the legend ) + labs(x = "Station", y = "Count") + theme_minimal()
2. Grayscale fill (your scale_fill_grey approach, fixed)
ggplot(IC, aes(x = factor(station), y = number, fill = factor(morning))) + geom_col(position = "stack") + scale_fill_grey( start = 0.5, end = 0, # Start at mid-gray, end at black labels = c("Group 11", "Group 22"), name = "Morning Group" ) + labs(x = "Station", y = "Count") + theme_minimal()
3. Gradient fill (if you really want it, though not ideal for discrete groups)
If you want to treat morning as a continuous value (even though it's just two points), use this:
ggplot(IC, aes(x = factor(station), y = number, fill = morning)) + geom_col(position = "stack") + scale_fill_gradient(low = "grey", high = "darkred") + # Use low/high for gradients labs(x = "Station", y = "Count", fill = "Morning Value") + theme_minimal()
All these should correctly apply colors to your stacked bars now—just make sure you're mapping fill to factor(morning) (or morning if using gradients) to tell ggplot which groups to color.
内容的提问来源于stack exchange,提问作者Sea24
相关产品推荐
相关产品推荐

