title: Adding line type to legend in ggplot2
body: <p>How do I make the line types used by geom_hline or geom_abline show up in the legend of a ggplot plot?</p>
  
  <p>For example:</p>
  
  <pre><code>require (ggplot2)
  
  # some data
  dummy &lt;- data.frame (category1 =  rep (1:5, 8), category2 = rep (1:4, each = 10), 
      category3 = rep (factor (1:2), 2), expected = 10 ^ rep (4:7, each = 10),
      value = 10 ^rnorm(40, 5))
  
  # faceted plot
  baseplot &lt;-ggplot (dummy ) +
      geom_point (aes (category1, value, color = category3))+
      scale_y_log10 () + 
      facet_wrap (~category2)
  
  # add a dotted line for expected value
  p1 &lt;-baseplot + geom_hline ( aes ( yintercept = expected), linetype = 2)
  </code></pre>
  
  <p>I tried a couple approaches to making the dotted line show up in the legend, but they give me the same thing as p1</p>
  
  <pre><code>p1a &lt; -p1+scale_linetype_discrete (labels = "expected")+
      guides ( linetype= guide_legend ("", labels ="expected"))
  p1b &lt;- baseplot + geom_hline (aes (yintercept = expected, linetype = "expected")) +
      scale_linetype_manual (labels= "expected", values = 2)
  p1a
  p1b
  </code></pre>
  
  <p>How about multiple lines/line types?</p>
  
  <p>Let's say I also wanted to plot groupwise and overall geometric means</p>
  
  <pre><code>require (reshape)
  require (plyr)
  
  # calculate geometric means, keep them in their own data frame
  geometric_mean &lt;- function (x)  exp ( mean (log (x)))
  dummy $GM_overall &lt;- geometric_mean (dummy $value)
  extra &lt;- ddply(dummy, c( "GM_overall", "expected","category2"), summarize,
      GM_group = geometric_mean (value))
  extra_long &lt;- melt (GM_group_long, id.vars = "category2")
  </code></pre>
  
  <p>I expected this approach to show linetype in the legend based on <a href="https://stackoverflow.com/questions/13254441/add-a-horizontal-line-to-plot-and-legend-in-ggplot2?rq=1">this post</a>, but no such luck</p>
  
  <pre><code>p2=baseplot + geom_hline ( aes ( yintercept = value , linetype = variable), extra)
  p2
  </code></pre>
  
  <p>Here's another case where I would want to do something similar with abline</p>
  
  <p>It would be nice to be able to label the line as 1:1</p>
  
  <pre><code>dummy$value2 &lt;- dummy $value * runif(40, 0.5, 2)
  ggplot (dummy)+coord_fixed() +    
      geom_point (aes (value, value2, color = category3))+
      geom_abline (yintercept =0, slope =1) 
  </code></pre>
  
  <p>I'm using R 3.0.0, ggplot 0.9.3.1 </p>