Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
502 views
in Technique[技术] by (71.8m points)

r - Adding legend to ggplot when lines were added manually

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("
Time-steps") + 
    ylab("Proportion correct
") + 
    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")
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...