R ggplot2 stat_summary图例表示组

时间:2016-05-17 08:27:00

标签: r ggplot2

我已经为整个研究人群(黑线)和男性和女性分别绘制了平均值。

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

传说只显示性别,有人可以帮我在整个小组的传奇中添加黑线和点吗?

enter image description here

1 个答案:

答案 0 :(得分:0)

您需要添加aes()才能获得黑线/点的图例。如果您希望图例用于线条/形状组合,则可以通过将guide = F添加到scale_shape_manual然后使用override.aes中的guides来指定形状的图例来指定形状的图例图例中的形状:

 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))))