r - Can colors and shape be changed on a combined line and point plot while maintaining one legend per graph? -
using following data frame:
day <- gl(8,1,48,labels=c("mon","tues","wed","thurs","fri","sat","sun","avg")) day <- factor(day, level=c("mon","tues","wed","thurs","fri","sat","sun","avg")) month<-gl(3,8,48,labels=c("jan","mar","apr")) month<-factor(month,level=c("jan","mar","apr")) snow<-gl(2,24,48,labels=c("y","n")) snow<-factor(snow,levels=c("y","n")) count <-c(.94,.95,.96,.98,.93,.94,.99,.9557143,.82,.84,.83,.86,.91,.89,.93,.8685714,1.07,.99,.86,1.03,.81,.92,.88,.9371429,.94,.95,.96,.98,.93,.94,.99,.9557143,.82,.84,.83,.86,.91,.89,.93,.8685714,1.07,.99,.86,1.03,.81,.92,.88,.9371429) d <- data.frame(day=day,count=count,month=month,snow=snow)
i'd change colors , shapes of lines , points grouped month on following graph:
library(ggplot2) library(scales) ggplot(data=d[d$day=="avg",],aes(x=day, y=count, fill=month,group=month,label=month),show_guide=f)+ facet_wrap(~snow,ncol=1,scales="free")+ geom_line(data=d[d$day!="avg",],aes(x=day, y=count, group=month, colour=month), show_guide=f)+ scale_x_discrete(limits=levels(d$day))+ scale_y_continuous(labels = percent)+ geom_point(aes(colour = month),size = 4,position=position_dodge(width=1.2))+ expand_limits(y=0)
- how can change colors , shapes (i realize many options exist) of both line graph , points still grouped month?
note: single legend per graph appropriate shape/color desired (bonus points if can figure out how create legend each graph without using grid.arrange).
i believe @thunk has right, there's more problems code. (1) specify fill
, don't use geoms take fill
. (2) set default aesthetics in first ggplot
, needlessly reset them in geom
s. (3) want shapes change, never specify shape aesthetic.
i think gives want:
ggplot(data=d[d$day=="avg",], aes(x=day, y=count, color=month, group=month, label=month, shape = month), show_guide=f)+ facet_wrap(~snow, ncol=1, scales="free")+ geom_line(data=d[d$day!="avg", ])+ scale_x_discrete(limits=levels(d$day))+ scale_y_continuous(labels = percent)+ geom_point(size = 4, position = position_dodge(width=1.2))+ scale_color_manual(values = c("dodgerblue4", "firebrick4", "forestgreen")) + expand_limits(y=0)
in first aes
call in initial ggplot()
specify both color
, shape
vary month. then, in geom_line
, geom_point
calls don't need again. adding scale_color_manual()
lets pick whatever colors want, (and if want specify shapes, adding scale_shape_manual()
work that).
hope helps!
Comments
Post a Comment