title: Normalizing y-axis in histograms in R ggplot to proportion by group
body: <p>My question is very similar to <a href="https://stackoverflow.com/questions/11766856/normalizing-y-axis-in-histograms-in-r-ggplot-to-proportion">Normalizing y-axis in histograms in R ggplot to proportion</a>, except that I have two groups of data of different size, and I would like that each proportion is relative to its group size instead of the total size.</p>
  
  <p>To make it clearer, let's say I have two sets of data in a data frame:</p>
  
  <pre><code>dataA&lt;-rnorm(100,3,sd=2)
  dataB&lt;-rnorm(400,5,sd=3)
  all&lt;-data.frame(dataset=c(rep('A',length(dataA)),rep('B',length(dataB))),value=c(dataA,dataB))
  </code></pre>
  
  <p>I can plot the two distributions together with:</p>
  
  <pre><code>ggplot(all,aes(x=value,fill=dataset))+geom_histogram(alpha=0.5,position='identity',binwidth=0.5)
  </code></pre>
  
  <p>and instead of the frequency on the Y axis I can have the proportion with:</p>
  
  <pre><code>ggplot(all,aes(x=value,fill=dataset))+geom_histogram(aes(y=..count../sum(..count..)),alpha=0.5,position='identity',binwidth=0.5)
  </code></pre>
  
  <p>But this gives the proportion relative to the total data size (500 points here): is it possible to have it relative to each group size?</p>
  
  <p>My goal here is to make it possible to compare visually the proportion of values in a given bin between A and B, independently from their respective size. Ideas which differ from my original one are also welcome!</p>
  
  <p>Thanks!</p>