To get better legend place color=variable
and linetype=variable
inside aes()
for the ggplot()
and for geom_vline()
- so there will be one legend. Then for geom_point()
place x and y inside aes()
as well as color="s.mean"
and linetype="s.mean"
. This will ensure that new level is added to legend. Now with scale_color"manual()
and scale_linetype_manual()
you can set desired colors and linetypes. With guides()
and override.aes=
you can remove points from first four entries.
ggplot(sdata.m, aes(x=value, color=variable,linetype=variable))+
stat_ecdf(size=1)+
geom_vline(data=ddata.m,
aes(xintercept = value,color=variable,linetype=variable),
size=2) +
geom_point(aes(x=mean(sdata.m$value[sdata.m$variable=="s.a"]),
color="s.mean",linetype="s.mean",y=.5),size = 5) +
geom_point(aes(x=mean(sdata.m$value[sdata.m$variable=="s.b"]),
color="s.mean",linetype="s.mean",y=.5),size = 5)+
scale_color_manual(breaks=c("d.a","d.b","s.a","s.b","s.mean"),
values=c("blue","blue","red","red","green"))+
scale_linetype_manual(breaks=c("d.a","d.b","s.a","s.b","s.mean"),
values=c(1,2,1,2,0))+
guides(color=guide_legend(override.aes=list(shape=c(NA,NA,NA,NA,16))))