how to bring the legend in ggplot

Hello,

I have wrote a code in Rstudio and it can plot the curves, however, I do not know how to bring its legends. Can you kindly guide me.

Thanks,

library(tidyverse)
require(gridExtra)

file11<-read.csv("F:/files/data1.CSV")
ggplot(data=a1, aes(x = No.)) + 
  geom_line(aes(y = ave1), colour="red",size=.8)+
  geom_line(aes(y = ave2), colour="blue", alpha=.2)+
  geom_line(aes(y = ave3), colour="coral4", alpha=.2)+
  geom_line(aes(y = ave4), colour=cl[8], alpha=.2)+
  geom_line(aes(y = ave5), colour="magenta", alpha=.2)+
  geom_line(aes(y = ave6), colour="darkolivegreen",size=.8)+
  xlab("age")+ylab("height")+
  theme(legend.position="bottom")

Is this what you are looking for?

library(ggplot2)

a1 <- data.frame(No. = 1:4, ave1 = 1:4, ave2 = 2:5, ave3 = 3:6,
                 ave4 = 4:7, ave5 = 5:8, ave6 = 6:9)

colors <- c("ave1" = "red", "ave2" = "blue", "ave3" = "coral4", "ave4" = "orange",
            "ave5" = "magenta", "ave6" = "darkolivegreen")
ggplot(data=a1, aes(x = No.)) + 
  geom_line(aes(y = ave1, colour= "ave1"),size=.8)+
  geom_line(aes(y = ave2, colour= "ave2"), alpha=.2)+
  geom_line(aes(y = ave3, colour= "ave3"), alpha=.2)+
  geom_line(aes(y = ave4, colour= "ave4"), alpha=.2)+
  geom_line(aes(y = ave5, colour= "ave5"), alpha=.2)+
  geom_line(aes(y = ave6, colour= "ave6"),size=.8)+
  xlab("age")+ylab("height")+
  theme(legend.position="bottom") +
  scale_color_manual(values = colors)

Created on 2020-05-21 by the reprex package (v0.3.0)

1 Like