Re: [R] Histogram with colors according to factor

From: Hans W. Borchers <>
Date: Tue, 08 Jul 2008 05:23:27 -0700 (PDT)

No; thanks for your try, but this is not what I want.

Here each bar has one single color. I would like to render each bar with several colors according to the distribution of a factor.

I now learned that this is called "stacked histogram" (damned Excel). In the following entry

Deepayan Sarkar has provided a solution though he doubts its value. Still, I find "stacked histograms" a vuable tool during exploratory data analysis.

Hans Werner

Is this what you want?

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() ggplot(dataset, aes(x = x, fill = y)) + geom_histogram(position = "dodge")

HTH, Thierry

Hans W. Borchers
ABB Corporate Research Germany
View this message in context:
Sent from the R help mailing list archive at

______________________________________________ mailing list
PLEASE do read the posting guide
and provide commented, minimal, self-contained, reproducible code.
Received on Tue 08 Jul 2008 - 12:29:56 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 - 13:31:10 GMT.

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

list of date sections of archive