24
votes

I have a plot in ggplot with 4 separate lines that I have added with a separate geom_line() argument. I would like to add legend but scale_colour_manual doesn't work in this case. What is the proper way to add legends when I added the variables separately?

Here's my code:

ggplot(proba[108:140,], aes(c,four)) + 
    geom_line(linetype=1, size=0.3) + 
    scale_x_continuous(breaks=seq(110,140,5)) + 
    theme_bw() + 
    theme(axis.line = element_line(colour = "black", size=0.25),
          panel.grid.major = element_blank(),
          panel.grid.minor = element_blank(),
          panel.border = element_blank(),
          panel.background = element_blank()) + 
    theme(axis.text.x = element_text(angle = 0, hjust = +0.5, size=6,color="black")) + 
    theme(axis.text.y = element_text(angle = 0, hjust = -100, size=6, color="black")) + 
    theme(axis.ticks=element_line(colour="black",size=0.25)) + 
    xlab("\nTime-steps") + 
    ylab("Proportion correct\n") + 
    theme(axis.text=element_text(size=8),axis.title=element_text(size=8)) + 
    geom_line(aes(c,three), size=0.2, linetype=2) + 
    geom_line(aes(c,one),linetype=3, size=0.8, colour="darkgrey") + 
    geom_line(aes(c,two), linetype=1, size=0.8, colour="darkgrey")
1
Can you post your code? Setting color="Line Name" in the call to aes should work.Peyton
I already have an argument in color but it doesn't display it as a legend. I have posted my code.user1723765
Put the color argument inside aes, and rather than setting it to the color name, set it to the name you want to appear in the legend. Then use scale_color_manual to map that name to the desired color.Peyton
I added color="black" inside aes and now it displays it in orange. also do I use scale_colour_manual(values=c("black"="name")?user1723765
I added an answer with an example.Peyton

1 Answers

25
votes

Just set the color name in aes to whatever the line's name on the legend should be.

I don't have your data, but here's an example using iris a line with random y values:

library(ggplot2)

line.data <- data.frame(x=seq(0, 10, length.out=10), y=runif(10, 0, 10))

qplot(Sepal.Length, Petal.Length, color=Species, data=iris) +
  geom_line(aes(x, y, color="My Line"), data=line.data)

enter image description here

The key thing to note is that you're creating an aesthetic mapping, but instead of mapping color to a column in a data frame, you're mapping it to a string you specify. ggplot will assign a color to that value, just as with values that come from a data frame. You could have produced the same plot as above by adding a Species column to the data frame:

line.data$Species <- "My Line"
qplot(Sepal.Length, Petal.Length, color=Species, data=iris) +
  geom_line(aes(x, y), data=line.data)

Either way, if you don't like the color ggplot2 assigns, then you can specify your own using scale_color_manual:

qplot(Sepal.Length, Petal.Length, color=Species, data=iris) +
  geom_line(aes(x, y, color="My Line"), data=line.data) +
  scale_color_manual(values=c("setosa"="blue4", "versicolor"="red4",
                              "virginica"="purple4", "My Line"="gray"))

enter image description here

Another alternative is to just directly label the lines, or to make the purpose of the lines obvious from the context. Really, the best option depends on your specific circumstances.