title: Let ggplot2 histogram show classwise percentages on y axis
body: <pre><code>library(ggplot2)
  data = diamonds[, c('carat', 'color')]
  data = data[data$color %in% c('D', 'E'), ]
  </code></pre>
  
  <p>I would like to compare the histogram of carat across color D and E, and use the classwise percentage on the y-axis. The solutions I have tried are as follows: </p>
  
  <p>Solution 1:</p>
  
  <pre><code>ggplot(data=data, aes(carat, fill=color)) +  geom_bar(aes(y=..density..), position='dodge', binwidth = 0.5) + ylab("Percentage") +xlab("Carat")
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/ygkYy.png" alt="enter image description here"></p>
  
  <p>This is not quite right since the y-axis shows the height of the estimated density.</p>
  
  <p>Solution 2: </p>
  
  <pre><code> ggplot(data=data, aes(carat, fill=color)) +  geom_histogram(aes(y=(..count..)/sum(..count..)), position='dodge', binwidth = 0.5) + ylab("Percentage") +xlab("Carat")
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/Ea2iM.png" alt="enter image description here"></p>
  
  <p>This is also not I want, because the denominator used to calculate the ratio on the y-axis are the total count of D + E.</p>
  
  <p>Is there a way to display the classwise percentages with ggplot2's stacked histogram? That is, instead of showing (# of obs in bin)/count(D+E) on y axis, I would like it to show (# of obs in bin)/count(D) and (# of obs in bin)/count(E) respectively for two color classes. Thanks.</p>