2

I've got the following code which is close to what I want but not quite:

p = ggplot(mtcars, aes_string(x="gear", fill="cyl"))
p = p + geom_histogram() + facet_grid(cyl~.) 
p

That plots a bunch of facets showing the histogram/density relative to the whole population in each facet grid but I'd actually like each facet grid's histogram to be the density relative to that group. How can I do this?

helloB
  • 3,472
  • 10
  • 40
  • 87
  • I've taken a look at your code: it looks like it *is* plotting a histogram for each group. Do you mean you want a different Y-scale for each facet, so they all look the same height? – Derwin McGeary Oct 23 '15 at 18:26
  • @DerwinMcGeary thanks for taking a look. There is a histogram per facet (in this case per cylinder count) but if you add up the densities in a particular facet, they don't sum to 1. I want the density with respect to other members of that facet/cylinder count, not with respect to the total count. – helloB Oct 23 '15 at 18:35
  • Even without facet_grid() the frequency plot of geom_histogram() doesn't sum up to 1. Try geom_density(). – dtrv Oct 23 '15 at 19:05
  • 2
    Possible duplicate of [Normalizing faceted histograms separately in ggplot2](http://stackoverflow.com/questions/16339204/normalizing-faceted-histograms-separately-in-ggplot2) – aosmith Oct 23 '15 at 20:01
  • [This](http://stackoverflow.com/questions/9614720/obtaining-percent-scales-reflective-of-individual-facets-with-ggplot2) is also pretty much the same thing, but note comments on answer that you'd need `binwidth = 1` for the `..density..` solution. – aosmith Oct 23 '15 at 20:03

0 Answers0