r - 在 ggplot 中指定条形图颜色
问题描述
我在 ggplot 中有一个条形图,我想在其中为条形图上色,它们落在哪个范围内。我绘制了 hlines 以显示我想要颜色变化的位置:0.4、0.5、0.6。我能找到的所有东西都会根据渐变进行着色。但是,我希望超过 0.6 的每个条都具有相同的绿色,低于 0.4 的每个条都具有相同的红色,等等。
attach(screen2D)
screen2D[order(screen2D[,2]),]
library(ggplot2)
ggplot(screen2D) +
geom_bar(aes(x = reorder(Target, -Median),
y = Median),
stat = "identity",
fill = "skyblue") +
geom_errorbar(aes(x = reorder(Target,
- Median),
ymin = Q1, ymax = Q3),
width = 0.25, colour = "black",
alpha = 0.9, size = 0.2) +
ggtitle("Title") +
xlab("X Label") +
ylab("Y Label") +
theme(plot.title = element_text(hjust = 0.5),
axis.text.x = element_text(angle = 90,
hjust = 1,
vjust = 0.25)) +
scale_y_continuous(expand = c(0,0),
limits = c(0,1)) +
geom_hline(yintercept = 0.40, linetype = "dashed",
color = "red") +
geom_hline(yintercept = 0.50, linetype = "dashed",
color = "red") +
geom_hline(yintercept = 0.60, linetype = "dashed",
color = "red")
绘图输出 inside
的fill
参数geom_bar
要求长度为 1 或与数据集的长度相同,在本例中为 102。有解决办法吗?
我认为这scale_fill_manual
对参数很有用breaks
,但这似乎只指定了一个单独的变量。
+ scale_fill_manual(
values = c("red", "yellow", "orange", "green"),
breaks = c(Median<0.4, 0.4<Median<0.5,
0.5<Median<0.6, 0.6<Median))
有没有办法实现这一点,而无需手动将数据集分成 4 个并将它们单独添加在一起?
解决方案
感谢@ValeriVoev 的评论。在将颜色逻辑列导入到 R 之前,将颜色逻辑列添加到电子表格中的明显选择是我所需要的。
推荐阅读
- excel - 将宏限制为活动工作表
- c - Initializing struct member of type struct pointer to null
- javascript - 下一条语句没有被执行 - jQuery
- javascript - java - 编辑页面的url参数并将编辑显示为同一页面中的超链接
- razor - razor core 中的多行文本显示
- angular - 如何从角度 http 客户端(角度 5)中的结果中获取布尔值?
- jquery - 如何在jQuery中分配负值
- json - Haskell,Aeson:使用部分不必要的值解析嵌套的 JSON
- node.js - 我可以在 Nodejs 项目中同时使用 ES5 和 Typescript 吗?
- java - ViewPagerAdapter 尝试在空对象引用上调用虚拟方法 Context.getSystemService(java.lang.String)'