首页 > 解决方案 > ggplot2 使用手动比例显示错误的颜色

问题描述

我正在尝试根据值使用手动色标绘制我的数据。但是,显示的颜色与我提供的值相对应。我的数据如下所示:

# A tibble: 100 x 2
   chunk      avg  
   <dbl>    <dbl>  
 1     0  0.0202
 2     1  0.0405
 3     2  0.0648
 4     3  0.0405
 5     4  0.0283
 6     5 -0.00806
 7     6 -0.0526
 8     7 -0.0364
 9     8 -0.00810
10     9  0.0243
# ... with 90 more rows

然后我将它传送到ggplot2:

data %>%
    ggplot(
        aes(
            chunk,
            avg,
            fill = cut(
                avg,
                c(-Inf, -0.01, 0.01, Inf)
            )
        )
    ) +
    geom_bar(stat = "identity", show.legend = FALSE) +
    scale_color_manual(
        values = c(
            "(-Inf, -0.01)" = "red",
            "[-0.01, 0.01]" = "yellow",
            "(0.01, Inf)" = "green"
        )
    )

如您所见,我想根据值对我的条形图着色,低于 -0.01 红色,高于 0.01 绿色和介于黄色之间。

这是我收到的结果:

失败的情节

我错过了什么?

标签: rggplot2colorsbar-chartscale-color-manual

解决方案


如果在 aes 中输入 'color=',则必须输入 scale_color_manual。但是,如果您按照实际操作进行填充,则命令为 scale_fill_manual


推荐阅读