r - 使用 data 中指定的颜色名称作为 geom_bar 中的填充颜色

标签 r ggplot2 fill

这个问题在这里已经有了答案:





Using a pre-defined color palette in ggplot

(4 个回答)


5年前关闭。




我有一个像这样的数据框(freqOvertimeHourlyData):

     HOURS INTERVAL BARCOLOR
1     147    16-17  "green"
2     150    17-18  "green"
3     144    18-19  "blue"
4     149    19-20  "red"
5     139    20-21  "red"
6     101    21-22  "red"
有没有办法创建条形图,使用 geom_bar ,根据实际颜色名称的填充颜色明确指定为数据集中的变量(BARCOLOR 列)?我目前的情节是这样制作的:
ggplot(freqOvertimeHourlyData, aes(x = INTERVAL, y = HOURS))+
geom_histogram(stat = "identity")+
theme(legend.position = "none")

最佳答案

如果您希望使用原始值而不进行缩放,以达到您的审美,那么 scale_identity可以使用。使用@Sandy Muspratt 回答中的“DF”:

ggplot(DF, aes(x = INTERVAL, y = HOURS, fill = BARCOLOR))+
  geom_bar(stat = "identity")+
  theme(legend.position = "none") +
  scale_fill_identity()

enter image description here

关于r - 使用 data 中指定的颜色名称作为 geom_bar 中的填充颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29272550/

相关文章:

opencv - 用cv:Mat填充一个std::vector

excel - 两个折线图和轴之间的阴影区域

rmarkdown/knitr : How to format function output as code?

r - 距离路径最近的点

删除 R 中 ggplot2 中的单个 x 轴刻度线?

r - ggplot2 - 如何使用 data.frame 中的列标记 x-ticks?

r - 为什么 R for 循环比使用 foreach 慢 10 倍?

r - 如何将字符串向量(每个给出命令)转换为 R 中的函数?

r - 为什么不应该在 R 包中使用 library() 或 require()

java - Java "Fill Image"函数中的 StackOverflowError