title: R ggplot - calculate intercept of multiple plotted curves and geom_hline
body: <p>I'm wanting to calculate interpolated x-intercepts for lines like these with geom_hline:</p>
  <p><a href="https://i.stack.imgur.com/L1OGT.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/L1OGT.png" alt="sample plot" /></a></p>
  <pre><code>library(dplyr)
  library(ggplot2)
  
  g1=data.frame(grp=&quot;1&quot;, x=seq(1,50,1), y=rnorm(50,5,1))
  g2=data.frame(grp=&quot;2&quot;, x=seq(1,30,1), y=rnorm(30,8,2))
  g3=data.frame(grp=&quot;3&quot;, x=seq(1,45,1), y=rnorm(45,10,1))
  comb.dat=rbind(g1,g2,g3)
  plot.dat=comb.dat %&gt;% group_by(grp) %&gt;% mutate(cum=cumsum(y)/sum(y))
  
  p1=ggplot(plot.dat, aes(x = x, y = cum, color=grp)) +geom_line()+ geom_hline(yintercept=.5, linetype=&quot;dashed&quot;, color = &quot;black&quot;)
  
  print(p1)
  
  hline=data.frame(plot.dat %&gt;% group_by(grp) %&gt;% mutate(test=cum&gt;0.49 &amp; cum&lt;0.51))#
  print(hline[hline$test==T,])# only works when an exact value in the range exists
  
  F1=approxfun(plot.dat$cum,plot.dat$x,ties=mean)
  #data.frame(plot.dat %&gt;% group_by(grp) %&gt;% mutate(F1(0.50)))#works only on first group
  
  g1b=plot.dat[plot.dat$grp==&quot;1&quot;,]
  F2=approxfun(g1b$cum,g1b$x)
  F2(0.5)#works but inefficient
  </code></pre>
  <p>I have a lot of these plots and I am looking for the most efficient way to find the x-intercepts with the horizontal line for each factor level so the intercept values can be compared to each other and annotated to the plot. I thought there was a logic approach but then I realized I need interpolation, probably using approxfun. I have not found a way to do this without breaking the groups out of the data frame and doing it one by one...Thanks for setting me straight.</p>