r - 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 绿色和介于黄色之间。
这是我收到的结果:
我错过了什么?
解决方案
如果在 aes 中输入 'color=',则必须输入 scale_color_manual。但是,如果您按照实际操作进行填充,则命令为 scale_fill_manual
推荐阅读
- python - 什么是连续 Python 脚本中数据库连接的好策略?
- css - 背景颜色的平滑过渡/ vue js
- php - 如何在 PHP 中递归地创建嵌套数组祖先
- c++ - 将当前日期和时间输出到 .txt 文件
- python-3.x - 如何使用递归函数显示 5 个块的堆栈?
- imagemagick - 使用 Imagemagick,每次使用时保存其变量
- microsoft-graph-api - Microsoft Graph API 报告 - 限制
- google-cloud-firestore - 延迟补偿和 Firestore 事务
- javascript - 推送通知的“TypeError:无法读取未定义的属性 'val'”
- ffmpeg - ffmpeg-python 修剪为什么不连接