title: Plot median values on top of a density distribution
body: <p>I'm trying to plot the median values of some data on a density distribution using the ggplot2 R library. I would like to print the median values as text <em>on top</em> of the density plot.</p>
  
  <p>You'll see what I mean with an example (using the "diamonds" default dataframe):</p>
  
  <p><a href="https://i.stack.imgur.com/bJxPD.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/bJxPD.png" alt="diamond price per cut"></a></p>
  
  <p>I'm printing three itmes: the density plot itself, a vertical line showing the median price of each cut, and a text label with that value. But, as you can see, the median prices overlap on the "y" axis (this aesthetic is mandatory in the geom_text() function).</p>
  
  <p>Is there any way to dynamically assign a "y" value to each median price, so as to print them at different heights? For example, at the maximum density value of each "cut".</p>
  
  <p>So far I've got this </p>
  
  <pre><code># input dataframe
  dia &lt;- diamonds
  
  # calculate mean values of each numerical variable:
  library(plyr)
  dia_me &lt;- ddply(dia, .(cut), numcolwise(median))
  
  ggplot(dia, aes(x=price, y=..density.., color = cut, fill = cut), legend=TRUE) +
    labs(title="diamond price per cut") +
    geom_density(alpha = 0.2) +
    geom_vline(data=dia_me, aes(xintercept=price, colour=cut),
               linetype="dashed", size=0.5) +
    scale_x_log10() +
    geom_text(data = dia_me, aes(label = price, y=1, x=price))
  </code></pre>
  
  <p>(I'm assigning a constant value to the y aesthetics in the geom_text function because it's mandatory)</p>