Reputation: 5456
I would like to change the color connecting the means in the following box-plots:
library(ggplot2)
box.df<-data.frame(myyear=rep(c(2012:2014),each=120),
mymonth=rep((1:12),times=10),
val=rnorm(120,4,1))
box.df$yyyymm<-box.df$myyear*100+box.df$mymonth
box.df$myyearnr<-as.numeric(as.factor(box.df$myyear))
p<-ggplot(box.df,aes(factor(yyyymm),val))+geom_boxplot()+aes(fill=factor(myyear))
p+theme(axis.text.x=element_text(angle=90,hjust=1))+
stat_summary(fun.y=mean,geom="line",aes(group=1,colour="yellow"))+
stat_summary(fun.y=mean,geom="point",aes(colour="yellow"))
.. the plot works in principle, but the connecting line is purple, not yellow. How can I rectify this?
Thx&kind regards
Upvotes: 1
Views: 299
Reputation: 1560
It is just a problem of aes
. This code works as you want:
library(ggplot2)
box.df<-data.frame(myyear=rep(c(2012:2014),each=120),
mymonth=rep((1:12),times=10),
val=rnorm(120,4,1))
box.df$yyyymm<-box.df$myyear*100+box.df$mymonth
box.df$myyearnr<-as.numeric(as.factor(box.df$myyear))
p<-ggplot(box.df, aes(factor(yyyymm), val)) +
geom_boxplot()+aes(fill=factor(myyear))
p+theme(axis.text.x=element_text(angle=90,hjust=1))+
stat_summary(fun.y=mean,geom="line",aes(group=myyear),color="yellow")+
stat_summary(fun.y=mean,geom="point",color="yellow")
Upvotes: 0
Reputation: 18435
The colour is not an aesthetic in this case, so just specify it separately...
p+theme(axis.text.x=element_text(angle=90,hjust=1))+
stat_summary(fun.y=mean,geom="line",aes(group=1),colour="yellow")+
stat_summary(fun.y=mean,geom="point",colour="yellow")
Upvotes: 1