tags:

views:

423

answers:

3

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

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.

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

A: 

How about using fill=..count.. or fill=I(..count..>9) right after y=..density..? You have to tinker with the legend title and labels a bit, but it gets the coloring right.

EDIT:
It seems I misunderstood your question a bit. If you want to define color based on the x-coordinate, you can use the ..x.. automatic variable similarly.

Aniko
Thank you for the good ideas! however - count's is not what I'am trying to show. I would like the bin-bar's color to depend on the value represented by that bin (not the count). i.e. not bins with more than 9 counts, but with values above 9.I don't think I am good at explaining this :-/Maybe it's my axis labels that confuses me. What I'd like is to color the bins to the right of the label "middle"
Andreas
A: 

What about scale_manual? Here's link to Hadley's site. I've used this function to set an appropriate fill colour for a boxplot. Not sure if it'll work with histogram, though...

aL3xa
+3  A: 

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()
hadley
Thank you - I changed the legendlabel like so (for posterity) scale_fill_grey("Name",breaks=c(FALSE,TRUE), labels=c("This", "That")).Another question though: - is it possible to do something like ..x.. >7, ..x..>9 (If I want three categories instead of only two?)
Andreas
In that case, use `cut`.
hadley
Thank you! - if anybody else is interested, this is how I did it: fill=cut(..x..,c(1,6,10))), binwidth=1, colour = "black", )+ scale_fill_grey( "name",breaks=c("(1,6]","(6,10]",NA), labels=c("Low", "Midle","High"))+
Andreas
Actually for that case, you're probably better off doing the transformation in the data, not in the plot. Also note the labels argument to cut.
hadley