如何在ggplot直方图中定义填充颜色? [英] how to define fill colours in ggplot histogram?

查看:128
本文介绍了如何在ggplot直方图中定义填充颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下简单的数据:

I have the following simple data

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
))

我想制作直方图:

from that I'd like to make a histogram:

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")

现在 - 我希望箱子根据价值采取色彩 - 例如价值> 9的箱子变得深灰色 - 其他所有物品都应该是浅灰色的。

Now - i'd like for the bins to take colou according to value - e.g. bins with value > 9 gets dark grey - everything else should be light grey.

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

I have tried with fill=statusgruppe, scale_fill_grey(breaks=9) etc. - but I can't get it to work. Any ideas?

推荐答案

希望这应该让你开始:

Hopefully this should get you started:

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()

这篇关于如何在ggplot直方图中定义填充颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆