Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does the ggplot legend show the "colour" parameter?

Tags:

r

legend

ggplot2

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?

like image 968
griffin Avatar asked Jan 19 '11 04:01

griffin


People also ask

How do I change the color of my legend in R?

To change the color legend element border using ggplot2, we can use theme function where can put color in legend. key argument to desired color with element_rect.

How do I get rid of Ggplot legend?

By specifying legend. position=”none” you're telling ggplot2 to remove all legends from the plot.

How do I get rid of Alpha legend ggplot2?

Example 1: Remove All Legends in ggplot2 We simply had to specify legend. position = “none” within the theme options to get rid of both legends.

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', ...))


2 Answers

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 of aes(). A slight modification of your code yields:

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 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 then melt() it into long format before plotting. That would shorten your call to ggplot() since you could get rid of the multiple calls to geom_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.

like image 125
Chase Avatar answered Nov 14 '22 23:11

Chase


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"))
like image 31
kohske Avatar answered Nov 14 '22 23:11

kohske