2016-05-17 114 views
0

我繪製了整個研究人羣(黑線)和男性和女性分別的平均值。R ggplot2 stat_summary legend爲組別

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

回答

0

您需要添加aes()得到的黑線/分的傳奇人物。如果你想傳說中加入guide = Fscale_shape_manual,然後使用guidesoverride.aes在圖例指定形狀成爲線/形狀的組合,你可以關閉傳說的形狀:

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

真棒,非常感謝你許多! – Laura