You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助: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?

  1. Missing fill mapping: The biggest issue is likely that you didn't explicitly map the morning column to the fill aesthetic in your plot. Without this, ggplot has no idea which groups to color differently.
  2. Confusing color vs fill: scale_color_* controls the color of lines/borders, while stacked bars use scale_fill_* for their interior color. Your scale_color_gradient call was targeting the wrong aesthetic.
  3. Incorrect gradient parameters: scale_color_gradient/scale_fill_gradient uses low and high to define gradient endpoints, not your group values like 11 or 22.
  4. Treating discrete values as continuous: Your morning column 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 09:01:49