2

I have a plot with 2 lines. Each line has its own color and type, however I failed trying to create the legend for this plot. I created 2 scale_..._manual, one for the line types, and other one for the colors, but then apperars 2 separated legends. How can I obtain a single legend with the line types and colors?

library(ggplot2)

x <- c(0,1,2,3,4,5)
y1 <- c(0,2,4,8,16,23)
y2 <- c(0,3,6,9,12,15)
DF <- data.frame(x,y1,y2)

ggplot(data = DF, aes(x = x, y = y1)) +
  geom_line(aes(linetype = "First", colour = "First")) +
  geom_line(aes(x = x, y = y2, linetype = "Second", colour = "Second")) +
  scale_linetype_manual(values = c("First" = 2, "Second" = 1)) +
  scale_colour_manual(values = c("First" = "red", "Second" = "blue"))

enter image description here

1
  • 2
    "use identical name and labels values for both [linetype] and colour scale"
    – Henrik
    Commented Dec 3, 2017 at 20:04

0