-1

I have this code:

ggplot() + 
  stat_smooth(data=fd.area.dfa.quant,aes(x=area,y=q50),alpha=0.6,colour="Red")+
  stat_smooth(data=fd.area.dfb.quant,aes(x=area,y=q50),alpha=0.6,colour="Green")+
  stat_smooth(data=fd.area.cfa.quant,aes(x=area,y=q50),alpha=0.6,colour="Purple")+
  stat_smooth(data=fd.area.csb.quant,aes(x=area,y=q50),alpha=0.6,colour="Blue")+
  stat_smooth(data=fd.area.bsk.quant,aes(x=area,y=q50),alpha=0.6,colour="Orange")+
  scale_x_log10("xlab",expand = c(0, 0),labels=comma)+
  scale_y_log10("ylab", expand = c(0, 0.05),labels=comma)+
  theme(aspect.ratio=1)+
  ggtitle("title")+
  ggsave("save2.png")

This is the figureenter image description here

I would like to

  1. Add a name each to the five lines (say: DFA,DFB,CFA,CSB,BSK)
  2. After that, I want to display the names in the show_guide with the corresponding color.

P.S. all lines are of various sizes and hence I am avoiding melting.

How can I do it?

Geekuna Matata
  • 1,349
  • 5
  • 19
  • 38

1 Answers1

5

Since you didn't share any sample data to make your code a minimal, reproducible example, I'm going to use my own and you may adapt it

d1<-data.frame(x=1:10, y=cumsum(runif(10)))
d2<-data.frame(x=1:10, y=cumsum(runif(10)))

ggplot() + 
  stat_smooth(data=d1,aes(x,y,colour="mya"),alpha=0.6)+
  stat_smooth(data=d2,aes(x,y,colour="myb"),alpha=0.6)+
  scale_color_manual(name="Line",values=c(mya="red",myb="green"))

To get this to work, we set the colour in the aes() call and assign it a unique (non-color) name. Then, we manually add a color scale and define a color value for each of the aes-color values we set. This will correctly create a legend for those values.

enter image description here

Community
  • 1
  • 1
MrFlick
  • 195,160
  • 17
  • 277
  • 295