Missing legend with ggplot2 and geom_line

put colour inside the aes like this:

d<-data.frame(x=1:5, y1=1:5, y2=2:6)

ggplot(d, aes(x)) + 
  geom_line(aes(y=y1, colour="1")) + 
  geom_line(aes(y=y2, colour="2")) +
  scale_colour_manual(values=c("red", "blue"))

but I recommend this way:

d2 <- melt(d, id="x")
ggplot(d2, aes(x, value, colour=variable)) + 
  geom_line() +
  scale_colour_manual(values=c("red", "blue"))

Leave a Comment