Why is the ggplot legend showing the color option?

I have the following example:

data <- structure(list(a = c(-1.25549186262767, -0.840855480786298, - 0.635371312524283, -0.602907981454667, -0.472166385166945, -0.285773634866154, 0.0701427664273268, 0.138108224803923, 1.38435934347858, 1.71144087270237), b = c(-3.44400412039417, 0.675644682353751, -1.04793816522475, -7.38303347186651, 2.34519166466874, 0.334780748573386, 4.76806919070976, 4.8633533150074, 3.50106026731172, -1.27172351054143), c = c(-3.02376206439776, -2.56390769080574, -1.48659913867609, -1.27976202274701, -0.368725655874139, 1.08537150160227, 3.98619381956471, 4.50687017428731, 4.10341582203292, -1.61769414438858 ), d = c(5.71851494232005, 2.90539833491649, 2.75195159216204, 2.73478241733301, 2.65941820902101, 2.60630235726839, 3.34836154776286, 3.62938300664006, 4.61153521538016, 5.56230567213863), e = c(8.98703236551896, 4.5660296657415, 4.32487774825464, 4.29789523068949, 4.17945528847841, 4.09598014088541, 5.26217626511884, 5.70382046327322, 7.24733897758039, 8.74153894964533)), .Names = c("a", "b", "c", "d", "e"), row.names = c(NA, -10L), class = "data.frame") ggplot(data, aes(x=a, y=b)) + geom_point() + geom_line(aes(x=a, y=c)) + geom_line(aes(x=a, y=(c - d), colour="red")) + geom_line(aes(x=a, y=(c + d), colour="red")) + geom_line(aes(x=a, y=(c - e), colour="blue")) + geom_line(aes(x=a, y=(c + e), colour="blue")) 

I want the labels to be "d" and "e", but instead they are assigned the value of the "color" field. Two questions:

1) How can I completely rule out a legend? 2) If I want to include a legend, how can I set it for certain values, and not for color?

+6
r ggplot2 legend
source share
3 answers

It is important to remember that elements inside aes() will display data in aesthetics. If you set a constant value (s), then you can move this information outside of aes() . A small modification to your code gives:

 ggplot(data, aes(x=a, y=b)) + geom_point() + geom_line(aes(x=a, y=c)) + geom_line(aes(x=a, y=(c - d)), colour="red") + geom_line(aes(x=a, y=(c + d)), colour="red") + geom_line(aes(x=a, y=(c - e)), colour="blue") + geom_line(aes(x=a, y=(c + e)), colour="blue") 

Which gives you what you need, without any legend. I like the @koshke approach to include the legend above, so we will not duplicate this. Another approach you could take was to manipulate the data outside of the ggplot2() call, and then melt() in a long format before plotting. This will reduce your call to ggplot() , since you can get rid of multiple geom_line() calls, but obviously the overhead of data preprocessing. Probably 6 in one, 1/2 a dozen in another for this problem, but something to keep in mind future problems.

+8
source share

Is this what you want to do?

 ggplot(data, aes(x=a, y=b)) + geom_point() + geom_line(aes(x=a, y=c)) + geom_line(aes(x=a, y=(c - d), colour="d")) + geom_line(aes(x=a, y=(c + d), colour="d")) + geom_line(aes(x=a, y=(c - e), colour="e")) + geom_line(aes(x=a, y=(c + e), colour="e")) + scale_colour_manual(name="legend title", values=c("red", "blue"), breaks=c("d", "e")) 
+5
source share

+ opts(legend.position = "none") can be done by adding the + opts(legend.position = "none") .

+2
source share

All Articles