1

I've plotted the mean for the whole study population (black line) and for men and women separately.

plotYYIR1<- ggplot(data=YYIR1Long, aes(x=TimeValue, y=YYIR1Value)) +
  labs(x="Week number", y="YYIR1 distance run (m)") +
  theme(plot.title = element_text(hjust = 0, vjust=0))+
  theme(legend.title=element_blank())+
  theme(legend.key.width = unit(1, "cm"))+
  stat_summary(fun.y = mean,geom = "point", size=2) + 
  stat_summary(fun.y = mean, geom = "line", size=0.7) +
  stat_summary(fun.y = mean,geom = "point", size=2, aes(shape=Sex,colour=Sex)) + 
  scale_shape_manual(values = c("Male"=17, "Female"=15))+
  stat_summary(fun.y = mean, geom = "line", size=0.7, aes(colour=Sex)) + 
  scale_colour_manual(values = c("#009CEF", "#CC0000"))+
  stat_summary(fun.data = mean_cl_normal, geom = "errorbar", width =2)+
  stat_summary(fun.data = mean_cl_normal, geom = "errorbar", width =2, aes(colour=Sex))
plotYYIR1

The legend only shows the genders, could someone help me with adding the black line and points in the legend for the whole group?

enter image description here

Sandy Muspratt
  • 31,719
  • 12
  • 116
  • 122
Laura
  • 13
  • 1
  • 4

1 Answers1

0

You need to add aes() to get a legend for the black line/points. If you want the legend to be for lines/shapes combined you can turn off the legend for shapes by adding guide = F to scale_shape_manual and then use override.aes in guides to specify the shapes in the legend:

 ggplot(data=YYIR1Long, aes(x=TimeValue, y=YYIR1Value)) +
  labs(x="Week number", y="YYIR1 distance run (m)") +
  theme(plot.title = element_text(hjust = 0, vjust=0))+
  theme(legend.title=element_blank())+
  theme(legend.key.width = unit(1, "cm"))+
  stat_summary(fun.y = mean,geom = "point", size=2, aes(colour = "mean")) + 
  stat_summary(fun.y = mean, geom = "line", size=0.7, aes(colour = "mean")) +
  stat_summary(fun.y = mean,geom = "point", size=2, aes(shape=Sex,colour=Sex)) + 
  scale_shape_manual(values = c("Male"=17, "Female"=15, "mean"=16), guide = F)+
  stat_summary(fun.y = mean, geom = "line", size=0.7, aes(colour=Sex)) + 
  scale_colour_manual(values = c("#009CEF", "#CC0000", "#000000"))+
  stat_summary(fun.data = mean_cl_normal, geom = "errorbar", width =2, aes(colour = "mean"))+
  stat_summary(fun.data = mean_cl_normal, geom = "errorbar", width =2, aes(colour=Sex)) +
  guides(colour = guide_legend(override.aes = list(shape = c("Male"=17, "Female"=15, "mean"=16)))) 
erc
  • 10,113
  • 11
  • 57
  • 88