r - 如何在ggplot直方图中定义填充颜色?

标签 r ggplot2

我有以下简单数据

data <- structure(list(status = c(9, 5, 9, 10, 11, 10, 8, 6, 6, 7, 10, 
10, 7, 11, 11, 7, NA, 9, 11, 9, 10, 8, 9, 10, 7, 11, 9, 10, 9, 
9, 8, 9, 11, 9, 11, 7, 8, 6, 11, 10, 9, 11, 11, 10, 11, 10, 9, 
11, 7, 8, 8, 9, 4, 11, 11, 8, 7, 7, 11, 11, 11, 6, 7, 11, 6, 
10, 10, 9, 10, 10, 8, 8, 10, 4, 8, 5, 8, 7), statusgruppe = c(0, 
0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, NA, 0, 1, 0, 1, 
0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 1, 0, 1, 
1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 
1, 0, 1, 1, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0)), .Names = c("status", 
"statusgruppe"), class = "data.frame", row.names = c(NA, -78L
))

从中我想制作一个直方图:
ggplot(data, aes(status))+
geom_histogram(aes(y=..density..),
     binwidth=1, colour = "black",
     fill="white")+
theme_bw()+
scale_x_continuous("Staus", breaks=c(min(data$status,na.rm=T), median(data$status, na.rm=T), max(data$status, na.rm=T)),labels=c("Low", "Middle", "High"))+
scale_y_continuous("Percent", formatter="percent")

现在 - 我希望垃圾箱根据值(value)采取颜色 - 例如值 > 9 的 bin 变为深灰色 - 其他所有内容都应为浅灰色。

我试过 fill=statusgruppe , scale_fill_grey(breaks=9)等等 - 但我无法让它工作。有任何想法吗?

最佳答案

希望这能让你开始:

ggplot(data, aes(status, fill = ..x..))+
  geom_histogram(binwidth = 1) + 
  scale_fill_gradient(low = "black", high = "white")

ggplot(data, aes(status, fill = ..x.. > 9))+
  geom_histogram(binwidth = 1) + 
  scale_fill_grey()

关于r - 如何在ggplot直方图中定义填充颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2546016/

相关文章:

r - 如何在仍然限制 x 轴范围的同时更改 x 轴刻度?

r - 根据R中的条件计算列之间的比率

r - 如何为ggplot2添加手动颜色(geom_smooth/geom_line)

r - 如何绘制时间序列的互相关矩阵?

css - 到达 css 节点时遇到问题

r - 如何在 ggplot2 的 geom_text() 中解析 LaTeX 数学公式

r - ggplot2的ggproto中 "non_missing_aes"的功能是什么?

r - 如何在绘制 partykit 的 ctree 输出时抖动节点分割字符串?

r - 如何使用 maptools、ggplot 或其他软件包在 R map 中添加城市和质心的名称?

python - 使用 write.xls() 保存多个数据帧时出错