0

I am trying to add a legend to the following combined ggplot. I have tried many times, but it looks like I won't succeed.

The legend should have 'Years' as title and than the years 1995 (which is green), 1996 (which is red) and 2000 (which is blue) with the combined colour of the line and the shape of the points.

Any help is welcome.

Thank you in advance!

####
library("ggplot2")
library("grid")
library("gridExtra")  
#####
#####
breaks.major <- c(105,135,165,195,225,255) 
breaks.minor <- c(90,120,150,180,210,240,270) 
labels.minor <- c("Apr","May","June","July","Aug","Sept") 
lims =c(90,270)
######
ggplot() +
# Green plot
geom_point(data=MeanL_1995, aes(x=SamDay, y=TL_mm), shape=15, colour="green", size=2.5)+
geom_line(data=MeanL_1995, aes(x=SamDay, y=TL_mm), shape=15, colour="green", size=0.5)+
# Red plot
geom_point(data=MeanL_1996, aes(x=SamDay, y=TL_mm), shape=16, color="red", size=2.5)+
geom_line(data=MeanL_1996, aes(x=SamDay, y=TL_mm), shape=16, color="red", size=0.5)+
# Blue plot
geom_point(data=MeanL_2000, aes(x=SamDay, y=TL_mm), shape=17, color="blue", size=2.5)+
geom_line(data=MeanL_2000, aes(x=SamDay, y=TL_mm), shape=17, color="blue", size=0.5)+
scale_x_continuous(limit = lims, minor_breaks = breaks.minor, breaks = breaks.major, labels = labels.minor)+
ylab("Mean length (mm)")+
xlab("")+ 
guides(colour = guide_legend(title="Years", shape = guide_legend(title="Years")))
Julius Vainora
  • 47,421
  • 9
  • 90
  • 102
Suusie
  • 149
  • 9
  • 1
    We are going to need `MeanL` datasets via `dput` as well. – Julius Vainora Oct 30 '18 at 16:17
  • What do you mean? – Suusie Oct 30 '18 at 16:21
  • 1
    While it's possible to answer your question by inspecting the code or by creating our own example data, it's potentially frustrating and much more time consuming. So, you will have more luck by providing a reproducible example (currently, we cannot run your code since we don't have your data). See https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Julius Vainora Oct 30 '18 at 16:23
  • ggplot isn't designed to be used like this. Make one dataframe with all your data (use bind_rows(..., .id = )) and then you will just need one geom_point and one geom_line and the legend will work automatically – Richard Telford Oct 30 '18 at 18:14
  • Indeed, it worked! I forged the data together into one dataframe and I got the legend! Thank you – Suusie Oct 31 '18 at 09:06

0 Answers0