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 they are instead being given the value of the "colour" field. Two questions:
1) How can I eliminate the legend entirely? 2) If I want to include the legend, how can I have it set to specific values rather than the colour?
Eliminating the the label could be done via adding
+ opts(legend.position = "none")
option also.is this what you want to do?
The important thing to remember here is that items inside of
aes()
will map data to aesthetics. If you are setting a constant value(s), then you can move that information outside ofaes()
. A slight modification of your code yields:Which gives you what you are after without any legend. I like @koshke's approach for including a legend above so won't duplicate that. The other approach you could take would be to perform your data manipulation outside of the call to
ggplot2()
and thenmelt()
it into long format before plotting. That would shorten your call toggplot()
since you could get rid of the multiple calls togeom_line()
, but there's obviously the overhead of preprocessing the data. Probably 6 in one, 1/2 dozen in the other for this problem, but something to keep in mind for future problems.