Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding legend to ggplot when lines were added manually

Tags:

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") 
like image 590
user1723765 Avatar asked Aug 05 '13 14:08

user1723765


People also ask

Why doesn't my Ggplot have a legend?

If you want to add a legend to a ggplot2 chart you will need to pass a categorical (or numerical) variable to color , fill , shape or alpha inside aes . Depending on which argument you use to pass the data and your specific case the output will be different.

How do I add a legend in ggplot2?

You can place the legend literally anywhere. To put it around the chart, use the legend. position option and specify top , right , bottom , or left . To put it inside the plot area, specify a vector of length 2, both values going between 0 and 1 and giving the x and y coordinates.

How do I change the legend value in ggplot2?

You can use the following syntax to change the legend labels in ggplot2: p + scale_fill_discrete(labels=c('label1', 'label2', 'label3', ...))

How do I get rid of the legend in ggplot2?

Example 1: Remove All Legends in ggplot2position = “none” within the theme options to get rid of both legends.


1 Answers

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.

like image 170
Peyton Avatar answered Oct 27 '22 06:10

Peyton