Stack Exchange
dashed line in ggplot legend
I want to set the linetype of my legend. 
My data is like: 
  VisitMonth VisitYear CaixaForum MNAC   FirstDay
  1:         01      2007         NA 7125 2007-01-01
  2:         02      2007         NA 5345 2007-02-01
  3:         03      2007         NA 4996 2007-03-01
  4:         04      2007         NA 5476 2007-04-01
  5:         05      2007         NA 6160 2007-05-01
 ---                                                
 98:         02      2015      17903 2360 2015-02-01
 99:         03      2015      30400 2930 2015-03-01
100:         04      2015      25422 3088 2015-04-01
101:         05      2015      10787 2130 2015-05-01
102:         06      2015       3679 2047 2015-06-01

I want to plot the time serie of CaixaForum and MNAC column. I have code the following:
ggplot(data = MUSEUMS, aes(x = FirstDay, y = MNAC)) + 
  geom_line(size=0.75, aes(x = FirstDay, y = MNAC, colour = "MNAC")) + 
  geom_line(size=0.75, aes(y = CaixaForum, colour = "CaixaForum"), linetype = "dashed") + 
  labs(title = "", x = "", y = "Monthly Visitors") +  theme_bw() + 
  theme(legend.title = element_text(size=16, face="bold"),  legend.direction = "horizontal",
        legend.position=c(0.5, 1.05), text = element_text(size=20)) + 
  scale_colour_manual(name="Legend",values=c(MNAC="black", CaixaForum="black")) 

As you can observe, you can not distinguish between the two linetypes in the legend: 

How can I fixed?
I coded other answers in stackoverflow but they haven't worked.

You can use melt from the reshape2 package to convert your data into the long format. You would get two columns variable and value. You then change your aesthetics to y=value and linetype=variable.

@drmariod ...probably you meant linetype = variable as well, if they want that in the legend too, which they appear to.

The bigger picture that you're missing is that things appear in the legend when you map aesthetics to a variable in your data inside of aes().

Yes, I was focused on color, not on linetype... changed it now and provided an example.

You could just switch to using linetype instead of color in the two point layers, as you're not actually using color for anything in your graphic.
It would look like this:
ggplot(data = MUSEUMS, aes(x = FirstDay, y = MNAC)) + 
    geom_line(size=0.75, aes(x = FirstDay, y = MNAC, linetype = "MNAC")) + 
    geom_line(size=0.75, aes(y = CaixaForum, linetype = "CaixaForum")) + 
    labs(title = "", x = "", y = "Monthly Visitors") +  theme_bw() + 
    theme(legend.title = element_text(size=16, face="bold"),  legend.direction = "horizontal",
            legend.position=c(0.5, 1.05), text = element_text(size=20)) + 
    scale_linetype_manual(name="Legend",values=c(MNAC="solid", CaixaForum="dashed")) 

If you really wanted to use the approach you are using now for some reason, you could get the lines you want via override.aes in guide_legend by adding the following line to your graphic:
guides(color = guide_legend(override.aes = list(linetype = c("solid", "dashed"))))


I was having the same problem. I think it has to do with the legend line being too small to show the dashes. Adding:
theme(legend.key.size=unit(3,"lines"))

fixed it for me.

I created some random data to demonstrate the solution...
library(reshape2)
df <- data.frame(a=rnorm(10), b=rnorm(10), x=1:10, other_data=rnorm(10))
mdf <- melt(df, id.vars='x', measure.vars=c('a','b'))
ggplot(mdf, aes(x, value, linetype=variable)) + geom_line()

