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. I looked into packages like 'lattice' and 'ggplot2'. Searching R-help revealed that 'histogram' is spelled 'histogramm' in almost 2% of the time. I now know how to color whole bars, but did not find a solution for this specific kind of visualization. Many thanks in advance, Hans Werner Borchers ---- Hans W. Borchers ABB Corporate Research ----- ---- Hans W. Borchers ABB Corporate Research Germany -- View this message in context: http://www.nabble.com/Histogram-with-colors-according-to-factor-tp18336930p18336930.html Sent from the R help mailing list archive at Nabble.com.
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 ------------------------------------------------------------------------ ---- ir. Thierry Onkelinx Instituut voor natuur- en bosonderzoek / Research Institute for Nature and Forest Cel biometrie, methodologie en kwaliteitszorg / Section biometrics, methodology and quality assurance Gaverstraat 4 9500 Geraardsbergen Belgium tel. + 32 54/436 185 Thierry.Onkelinx op inbo.be www.inbo.be To call in the statistician after the experiment is done may be no more than asking him to perform a post-mortem examination: he may be able to say what the experiment died of. ~ Sir Ronald Aylmer Fisher The plural of anecdote is not data. ~ Roger Brinner The combination of some data and an aching desire for an answer does not ensure that a reasonable answer can be extracted from a given body of data. ~ John Tukey -----Oorspronkelijk bericht----- Van: r-help-bounces op r-project.org [mailto:r-help-bounces op r-project.org] Namens Hans W. Borchers Verzonden: dinsdag 8 juli 2008 13:23 Aan: r-help op r-project.org Onderwerp: [R] Histogram with colors according to factor 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. I looked into packages like 'lattice' and 'ggplot2'. Searching R-help revealed that 'histogram' is spelled 'histogramm' in almost 2% of the time. I now know how to color whole bars, but did not find a solution for this specific kind of visualization. Many thanks in advance, Hans Werner Borchers ---- Hans W. Borchers ABB Corporate Research ----- ---- Hans W. Borchers ABB Corporate Research Germany -- View this message in context: http://www.nabble.com/Histogram-with-colors-according-to-factor-tp183369 30p18336930.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ R-help op r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
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 https://stat.ethz.ch/pipermail/r-help/2007-April/129645.html 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: http://www.nabble.com/Histogram-with-colors-according-to-factor-tp18336930p18337935.html Sent from the R help mailing list archive at Nabble.com.
After reading your question again I came up with these plots. dataset <- data.frame(x = c(rnorm(1000), rnorm(200, mean = 1)), y gl(2, 100, labels = LETTERS[1:2])) ggplot(dataset, aes(x = x, group = y)) + stat_bin(aes(fill = ..count..), width = 0.2) + scale_fill_gradient(low = "red", high = "green") ggplot(dataset, aes(x = x)) + stat_bin(aes(fill = ..ncount..), width 0.2) + scale_fill_gradient(low = "red", high = "green") + facet_grid(. ~ y) ggplot(dataset, aes(x = x)) + stat_bin(aes(fill = ..count..), width 0.2) + scale_fill_gradient(low = "red", high = "green") + facet_grid(. ~ y) HTH, Thierry ------------------------------------------------------------------------ ---- ir. Thierry Onkelinx Instituut voor natuur- en bosonderzoek / Research Institute for Nature and Forest Cel biometrie, methodologie en kwaliteitszorg / Section biometrics, methodology and quality assurance Gaverstraat 4 9500 Geraardsbergen Belgium tel. + 32 54/436 185 Thierry.Onkelinx op inbo.be www.inbo.be To call in the statistician after the experiment is done may be no more than asking him to perform a post-mortem examination: he may be able to say what the experiment died of. ~ Sir Ronald Aylmer Fisher The plural of anecdote is not data. ~ Roger Brinner The combination of some data and an aching desire for an answer does not ensure that a reasonable answer can be extracted from a given body of data. ~ John Tukey -----Oorspronkelijk bericht----- Van: r-help-bounces op r-project.org [mailto:r-help-bounces op r-project.org] Namens Hans W. Borchers Verzonden: dinsdag 8 juli 2008 14:23 Aan: r-help op r-project.org Onderwerp: Re: [R] Histogram with colors according to factor 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 https://stat.ethz.ch/pipermail/r-help/2007-April/129645.html 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: http://www.nabble.com/Histogram-with-colors-according-to-factor-tp183369 30p18337935.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ R-help op r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
> 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") Hadley -- http://had.co.nz/