title: Scale geom_density to match geom_bar with percentage on y
body: <p>Since I was confused about the math <a href="https://stackoverflow.com/questions/32412805/ggplot2-histogram-with-density-curve-that-sums-to-1">last time I tried asking this</a>, here's another try. I want to combine a histogram with a smoothed distribution fit. And I want the y axis to be in percent.</p>
  
  <p>I can't find a <em>good</em> way to get this result. Last time, I managed to find a way to scale the <code>geom_bar</code> to the same scale as <code>geom_density</code>, but that's the opposite of what I wanted.</p>
  
  <p>My current code produces this output:</p>
  
  <pre><code>ggplot2::ggplot(iris, aes(Sepal.Length)) +
    geom_bar(stat="bin", aes(y=..density..)) +
    geom_density()
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/wWt6l.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/wWt6l.png" alt="enter image description here"></a></p>
  
  <p>The density and bar y values match up, but the scaling is nonsensical. I want percentage on the y axes, not well, the density.</p>
  
  <p>Some new attempts. We begin with a bar plot modified to show percentages instead of counts:</p>
  
  <pre><code>gg = ggplot2::ggplot(iris, aes(Sepal.Length)) +
    geom_bar(aes(y = ..count../sum(..count..))) +
    scale_y_continuous(name = "%", labels=scales::percent)
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/1CL3m.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/1CL3m.png" alt="enter image description here"></a></p>
  
  <p>Then we try to add a geom_density to that and somehow get it to scale properly:</p>
  
  <pre><code>gg + geom_density()
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/8yoym.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/8yoym.png" alt="enter image description here"></a></p>
  
  <pre><code>gg + geom_density(aes(y=..count..))
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/uzGgx.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/uzGgx.png" alt="enter image description here"></a></p>
  
  <pre><code>gg + geom_density(aes(y=..scaled..))
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/OO2In.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/OO2In.png" alt="enter image description here"></a></p>
  
  <pre><code>gg + geom_density(aes(y=..density..))
  </code></pre>
  
  <p>Same as the first.</p>
  
  <pre><code>gg + geom_density(aes(y = ..count../sum(..count..)))
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/dp8a5.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/dp8a5.png" alt="enter image description here"></a></p>
  
  <pre><code>gg + geom_density(aes(y = ..count../n))
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/vpp8U.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/vpp8U.png" alt="enter image description here"></a></p>
  
  <p>Seems to be off by about factor 10...</p>
  
  <pre><code>gg + geom_density(aes(y = ..count../n/10))
  </code></pre>
  
  <p>same as:</p>
  
  <pre><code>gg + geom_density(aes(y = ..density../10))
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/EeBef.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/EeBef.png" alt="enter image description here"></a></p>
  
  <p>But ad hoc inserting numbers seems like a bad idea.</p>
  
  <p>One useful trick is to inspect the calculated values of the plot. These are not normally saved in the object if one saves it. However, one can use:</p>
  
  <pre><code>gg_data = ggplot_build(gg + geom_density())
  gg_data$data[[2]] %&gt;% View
  </code></pre>
  
  <p>Since we know the density fit around x=6 should be about .04 (4%), we can look around for ggplot2-calculated values that get us there, and the only thing I see is density/10.</p>
  
  <p>How do I get <code>geom_density</code> fit to scale to the same y axis as the modified <code>geom_bar</code>?</p>
  
  <p>Bonus question: why are the grouping of the bars different? The current function does not have spaces in between bars.</p>