title: How to add horizontal lines showing means for all groups in ggplot2?
body: <p>Is there a way to place horizontal lines with the group means on a plot without creating the summary data set ahead of time? I know this works, but I feel there must be a way to do this with just <code>ggplot2</code>.</p>
  
  <pre><code>library(dplyr)
  library(ggplot2)
  X &lt;- data_frame(
    x = rep(1:5, 3),
    y = c(rnorm(5, 5, 0.5),
          rnorm(5, 3, 0.3),
          rnorm(5, 4, 0.7)),
    grp = rep(LETTERS[1:3], each = 5))
  
  X.mean &lt;- X %&gt;%
    group_by(grp) %&gt;%
    summarize(y = mean(y))
  
  X %&gt;%
    ggplot(aes(x = x, y = y, color = grp)) +
    geom_point(shape = 19) +
    geom_hline(data = X.mean, aes(group = grp, yintercept = y, color = grp)) +
    background_grid()
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/2txJo.png" rel="noreferrer"><img src="https://i.stack.imgur.com/2txJo.png" alt="grouped mean lines"></a></p>