title: Normalizing y-axis in density plots in R ggplot to proportion by group
body: <p>My question is very similar to </p>
  
  <p><a href="https://stackoverflow.com/questions/22181132/normalizing-y-axis-in-histograms-in-r-ggplot-to-proportion-by-group">Normalizing y-axis in histograms in R ggplot to proportion by group</a></p>
  
  <p>Except, I need density plots and I would like to have the y-axis as a rate like x counts per 1000 patients.</p>
  
  <p>I have multiple groups of data of different sizes, 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>
  
  <p>example data:</p>
  
  <pre><code>dataA&lt;-rnorm(10000,3,sd=2)
  dataB&lt;-rnorm(40000,5,sd=3)
  bp_combi&lt;-data.frame(dataset=c(rep('A',length(dataA)),rep('B',length(dataB))),
                       value=c(dataA,dataB))
  </code></pre>
  
  <p>I can plot the distributions together relative to the total size, but not to the relative size.</p>
  
  <pre><code>combi_dens = ggplot(bp_combi, 
                      aes(x=value, 
                          number_of_cases=nrow(bp_combi),
                          y=(..count..)/number_of_cases*1000, fill=dataset)) +
                 geom_density(bw = 1, alpha=0.4, size = 1.5 )
  </code></pre>
  
  <p>is it possible to have it relative to each group size?</p>
  
  <p>Thanks!</p>