Re: [R] Histogram with colors according to factor

From: hadley wickham <>
Date: Tue, 08 Jul 2008 08:55:45 -0500

> Given a data frame with a continuous variable and a factor. I would like to
> generate a histogram of the continuous variable, where each bar is filled
> with different colors according to the percentage of factor values falling
> into this region of the continuous variable.

How exactly do you want the bar coloured? I can see that this might possibly work if the factor has only two levels, but how is a colour supposed to encode the a categorical distribution?

Or do you mean:

dataset <- data.frame(x = c(rnorm(100), runif(100), rchisq(100, 1)), y = gl(3, 100, labels = LETTERS[1:3]))
ggplot(dataset, aes(x = x, fill = y)) + geom_histogram(position = "fill")



______________________________________________ mailing list
PLEASE do read the posting guide
and provide commented, minimal, self-contained, reproducible code.
Received on Tue 08 Jul 2008 - 13:59:07 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 08 Jul 2008 - 14:31:12 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.

list of date sections of archive