1

I'd like to change the title of the legend generated using the code below. Note, I know I could put the data.frame and aesthetic directly in the ggplot() function call, but I don't in this example because in my application, I need multiple layers with different datasets and aesthetics.

d = data.frame(t=rep(1:10,2),
               value = rep(1:2,each=10),
               dose_mpk = factor(rep(1:2,each=10)))

g = ggplot() 
a = aes(x=t,y=value,group=dose_mpk,color=dose_mpk,shape=dose_mpk)
g = g + geom_point(data=d,a)
g = g + geom_line(data=d,a)
print(g)

enter image description here

I don't understand the right way to change the legend title. I tried the code below, which gives the plot below, which is not what I want - I just wanted to change the title, not create double legends. Since my original legend refers to both a shape and color, I'm not sure how to change this "combined" title. What should I do?

g + guides(color = guide_legend("dose (mg/kg)"))

enter image description here

Andy Stein
  • 461
  • 1
  • 4
  • 11

1 Answers1

5

If the legends have different titles, they can not be combined. Simply set both legends to the same title:

g + guides(color = guide_legend("dose (mg/kg)"), 
           shape = guide_legend("dose (mg/kg)"))

Instead of guides(), you could also use labs(),:

g + labs(color = "dose (mg/kg)", shape = "dose (mg/kg)")
Stibu
  • 15,166
  • 6
  • 57
  • 71