16

I am looking for a way to hide one of the aestetic legends from the plot created with the code below. To scale the point color by date, I had to convert the dates into numbers, and I'd rather not show the date legend on the plot. On the other hand, the shape legend is important information to display. I understand that legend.position="none" will completely remove the legend, but then that leaves me with the problem of how to communicate the meaning behind the shapes.

library(ggplot2)
w<-read.table("data.txt", header=TRUE)
pt.data <- w[w$dt==min(w$dt),]
p <- ggplot(data=w, aes(OAD,RtgValInt,color=dt,shape=Port)) +
     geom_jitter(size=3, alpha=0.75) +
     scale_colour_gradient(limits=c(min(w$dt), 
             max(w$dt)),
         low="#9999FF", high="#000066") +
     geom_point(data=pt.data, 
         color="red", size=3, aes(shape=Port))
print(p)

The data.txt file includes the lines below.

Date          Port    OAD         RtgValInt   dt
12/31/2010  Grp1    1.463771    1.833333    14974
12/31/2010  Grp2    1.193307    2.071429    14974
11/30/2010  Grp1    1.454115    1.833333    14943
11/30/2010  Grp2    1.127755    2.071429    14943
10/29/2010  Grp1    1.434965    2.000000    14911
10/29/2010  Grp2    1.055758    2.071429    14911
09/30/2010  Grp1    1.441773    2.000000    14882
09/30/2010  Grp2    1.077799    2.071429    14882

enter image description here

user338714
  • 2,315
  • 5
  • 27
  • 36

1 Answers1

10

You can suppress the legends at the layer level. If you move the colour aesthetic from the initial call to ggplot to the jitter layer, that seems to give the effect you are after. I'm a bit confused though as to why you would want to colour based on date and not provide the key as to what the colors mean...but that's a more philosophical question for you to ponder.

ggplot(data=w, aes(OAD,RtgValInt,shape=Port)) +
  geom_jitter(size=3, alpha=0.75, color=dt, legend = FALSE) +
  scale_colour_gradient(limits=c(min(w$dt), max(w$dt)),low="#9999FF", high="#000066") +
  geom_point(data=pt.data, color="red", size=3, aes(shape=Port))
Chase
  • 67,710
  • 18
  • 144
  • 161
  • 1
    Although this removes the data legend, it also removes the color scaling from the points. The dates represent fixed historical periods (e.g. 1 month) and their actual values aren't as import and the trend over time. – user338714 Mar 24 '11 at 17:59
  • 15
    I think you probably actually want to suppress the legend at the scale level: `+ scale_colour_gradient(..., legend = F)` – hadley Mar 24 '11 at 19:51
  • 5
    the legend field is deprecated as of 0.9.0, you can use the show_guide field instead. – momeara May 13 '12 at 22:48
  • 2
    The `show_guide` field is deprecated. Try `scale_colour_gradient(..., guide = "none")` instead. – Zoë Clark May 27 '15 at 23:22
  • @ZoëClark - feel free to update and edit the answer to make it more relevant...this was answered over four years ago, ggplot2 has grown up a lot since then! – Chase May 27 '15 at 23:42