title: ggplot2: How to create dashed lines in legend?
body: <p>I am trying to create a ggplot consisting of 2 densities with straight lines and 2 dashed densities. 2 densities are blue and the other 2 are red. My plot should include a legend with colors and lines in respect to these densities.</p>
  
  <p>Even though I think there must be an easy solution, I am not able to produce dashed lines in my legend. I tried to solve my issue based on several threads, e.g. <a href="https://stackoverflow.com/questions/14875582/changing-the-line-type-in-the-ggplot-legend">here</a> or <a href="https://stackoverflow.com/questions/13572572/ggplot2-dashed-line-in-legend">here</a>, but my ggplot code is already kind of complex and the examples of these threads use ggplot in different ways. Hence, I didn't manage to reproduce the results of these threads.</p>
  
  <p>Here is a reproducible example of what I did so far. The last line with <code>scale_linetype_manual</code> seems not to work. How could I produce dashed lines in my legend for grp3 and grp4?</p>
  
  <pre><code>library(ggplot2)
  
  set.seed(123)
  
  # Example data
  N &lt;- 1000
  x1 &lt;- rnorm(N, 5)
  x2 &lt;- rnorm(N, 10)
  x3 &lt;- rnorm(N, 15)
  x4 &lt;- rnorm(N, 20)
  
  ggplot() + 
    stat_density(aes(x = x1, col = "grp1"), position = "identity", geom = "line") + 
    stat_density(aes(x = x2, col = "grp2"), position = "identity", geom = "line") +
    stat_density(aes(x = x3, col = "grp3"), position = "identity", geom = "line", linetype = 2) +
    stat_density(aes(x = x4, col = "grp4"), position = "identity", geom = "line", linetype = 2) +
    theme_bw() +
    theme(axis.text.y = element_text(angle = 90, hjust = 0.3), 
          axis.title.x = element_text(vjust = - 0.5), 
          plot.title = element_text(vjust = 1.5), 
          legend.title = element_blank(), legend.position = c(0.75, 0.85), 
          legend.key = element_blank(), legend.text = element_text(size = 10)) + 
    scale_color_manual(values = c("red", "dodgerblue3", "red", "dodgerblue3")) + 
    scale_linetype_manual(values = c("grp1" = 1, "grp2" = 1, "grp3" = 2, "grp4" = 2))
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/6JCew.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/6JCew.jpg" alt="enter image description here"></a></p>