title: Combining linetype and color in ggplot2 legend
body: <p>I have a dataframe like this :</p>
  
  <pre><code>&gt; head(df_graph)
     treatment year      mean         se
  1:        AC 2005 0.3626147 0.03005057
  2:        AC 2006 0.3925465 0.02370335
  3:        AC 2007 0.3217444 0.02279881
  4:        AC 2008 0.3895656 0.05985077
  5:        AC 2009 0.3820191 0.01481586
  6:        AC 2010 0.3732695 0.03544626
  ...
  </code></pre>
  
  <p>And a (long) ggplot script :</p>
  
  <pre><code>df_graph %&gt;% 
  
    # way to make 2 lines becoming 4 in a smooth way
    filter(treatment %in% c("Ambient", "Elevated")) %&gt;%
    mutate(treatment = ifelse(treatment == "Ambient", "AA", "EE")) %&gt;%
    bind_rows(df_graph) %&gt;%
    mutate(treatment_group = ifelse(treatment %in% c("Ambient", "AC", "AF", "AA"),"treatment1","treatment2"),
           line_type = ifelse(treatment %in% c("AA", "EE", "AF", "EF"),"type1","type2")) %&gt;%
  
    # plot
    ggplot(aes(x = year, y = mean,group = interaction(treatment_group, line_type),color = treatment_group)) +
    geom_line(aes(x = year, y = mean, linetype = line_type),size = 1.5, lineend = "round") +
    geom_point(size=5)+
    geom_errorbar(aes(ymin = mean-se, ymax = mean+se),width = 0.2, size = 1.5)+
  
    # scaling visual
    scale_color_manual(values=c('blue1','red3'))+
    scale_linetype_manual(values = c('dashed', 'solid'))+
    scale_x_continuous(breaks = c(1999:2010), limits = c(1998.5, 2010.5),labels = 1999:2010)+
  
    # axes and legend
    labs(title ="", x="year", y = expression(paste("result")))+
    theme_classic() + theme(text = element_text(size=20))
  </code></pre>
  
  <p>I made it this way so that 2 treatments can become 4 after 2004. My problem concerns my legend. By running this script, I get a 'two parts' legend with 1) the color (treatment_group) and the 2) the line type (line_type).</p>
  
  <p>What I need is a legend with only the 4 treatments displayed after 2004.</p>
  
  <p>What I get:
  <a href="https://i.stack.imgur.com/K7lfI.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/K7lfI.png" alt="wrong legend"></a></p>
  
  <p>What I would like to get (ideally):
  <a href="https://i.stack.imgur.com/FMmnf.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/FMmnf.png" alt="better legend"></a></p>
  
  <p>I realize my dataframe is not the best format, but to get a smooth transition from 2004 to 2005, that's the only way I found. Hence a good solution would be changing the ggplot script, not the shape of the dataframe.</p>
  
  <p>I've seen this : <a href="https://stackoverflow.com/questions/11344561/controlling-line-color-and-line-type-in-ggplot-legend">Controlling line color and line type in ggplot legend</a></p>
  
  <p>But it will also add the 'ambient' and 'elevated' treatment, and so duplicate the straight lines in the legend. Thank you for your help.</p>