24

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")
joran
  • 169,992
  • 32
  • 429
  • 468
user1723765
  • 6,179
  • 18
  • 57
  • 85
  • Can you post your code? Setting `color="Line Name"` in the call to `aes` should work. – Peyton Aug 05 '13 at 14:12
  • I already have an argument in color but it doesn't display it as a legend. I have posted my code. – user1723765 Aug 05 '13 at 14:15
  • 4
    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 Aug 05 '13 at 14:16
  • 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 Aug 05 '13 at 14:21
  • I added an answer with an example. – Peyton Aug 05 '13 at 14:39
  • This is almost a duplicate of http://stackoverflow.com/questions/10349206/add-legend-to-ggplot2-line-plot - the basic answer is you should melt your data. Getting your data in the right shape makes ggplot easy. If for some reason you don't want to do that, the second answer on that link shows you how to get the legend in your situation. – alexwhan Aug 07 '13 at 12:44

1 Answers1

25

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.

Peyton
  • 7,266
  • 2
  • 29
  • 29
  • 2
    this works for the first variable that's in the first aes() argument in the above code, but for the additional geom_line()s – user1723765 Aug 05 '13 at 14:40
  • Is this not working for you? Can you post your code and data both--basically an example that I can run to see where you're at? – Peyton Aug 06 '13 at 02:12