title: How to merge color, line style and shape legends in ggplot
body: <p>Suppose I have the following plot in ggplot:</p>
  
  <p><a href="https://i.stack.imgur.com/YwRpg.png" rel="noreferrer"><img src="https://i.stack.imgur.com/YwRpg.png" alt="ggplot graph"></a></p>
  
  <p>It was generated using the code below:</p>
  
  <pre><code>x &lt;- seq(0, 10, by = 0.2)
  y1 &lt;- sin(x)
  y2 &lt;- cos(x)
  y3 &lt;- cos(x + pi / 4)
  y4 &lt;- sin(x + pi / 4)
  df1 &lt;- data.frame(x, y = y1, Type = as.factor("sin"), Method = as.factor("method1"))
  df2 &lt;- data.frame(x, y = y2, Type = as.factor("cos"), Method = as.factor("method1"))
  df3 &lt;- data.frame(x, y = y3, Type = as.factor("cos"), Method = as.factor("method2"))
  df4 &lt;- data.frame(x, y = y4, Type = as.factor("sin"), Method = as.factor("method2"))
  
  df.merged &lt;- rbind(df1, df2, df3, df4)
  
  ggplot(df.merged, aes(x, y, colour = interaction(Type, Method), linetype = Method, shape = Type)) + geom_line() + geom_point()
  </code></pre>
  
  <p>I would like to have only one legend that correctly displays the shapes, the colors and the line types (the interaction(Type, Method) legends is the closest to what I would like, but it does not have the correct shapes/line types).</p>
  
  <p>I know that if I use scale_xxx_manual and I specify the same labels for all legends they will be merged, but I don't want to have to set the labels manually: <strong>if there are new Methods or Types, I don't want to have to modify my code</strong>: a want something generic.</p>
  
  <h1>Edit</h1>
  
  <p>As pointed in answers below, there are several ways to get the job done in this particular case. All proposed solutions require to manually set the legend line types and shapes, either by using <code>scale_xxx_manual function</code>s or with <code>guides</code> function.</p>
  
  <p>However, the proposed solutions still don't work in the general case: for instance, if I add a new data frame to the data set with a new "method3" Method, it does not work anymore, we have to manually add the new legend shapes and line types:</p>
  
  <pre><code>y5 &lt;- sin(x - pi / 4)
  df5 &lt;- data.frame(x, y = y5, Type = as.factor("sin"), Method = as.factor("method3"))
  df.merged &lt;- rbind(df1, df2, df3, df4, df5)
  override.shape &lt;- c(16, 17, 16, 17, 16)
  override.linetype &lt;- c(1, 1, 3, 3, 4)
  
  g &lt;- ggplot(df.merged, aes(x, y, colour = interaction(Type, Method), linetype = Method, shape = Type)) + geom_line() + geom_point()
  g &lt;- g + guides(colour = guide_legend(override.aes = list(shape = override.shape, linetype = override.linetype)))
  g &lt;- g + scale_shape(guide = FALSE)
  g &lt;- g + scale_linetype(guide = FALSE)
  print(g)
  </code></pre>
  
  <p>This gives:</p>
  
  <p><a href="https://i.stack.imgur.com/RCpfy.png" rel="noreferrer"><img src="https://i.stack.imgur.com/RCpfy.png" alt="5 curves"></a></p>
  
  <p>Now the question is: how to automatically generate the <code>override.shape</code> and <code>override.linetype</code> vectors?</p>
  
  <p>Note that the vector size is 5 because we have 5 curves, while the <code>interaction(Type, Method)</code> factor has size 6 (I don't have data for the cos/method3 combination)</p>