3
votes

I have a plot in which I am displaying individual values from multiple subjects, coloured by group. Added to that are means per group, calculated using stat_summary.

I would like the two means to be coloured by group, but in colours other than the individual data. This turns out to be difficult, at least when using stat_summary. I have the following code:

ggplot(data=dat, 
       aes(x=Round, y=DV, group=Subject, colour=T1)) + 
  geom_line() + geom_point() + theme_bw() +
  stat_summary(fun.y=mean, geom="line", size=1.5,
               linetype="dotted", color="black",
               aes(group=T1))

Which produces this example graph.

The colour for the means created by stat_summary is set to black; otherwise it would be red and blue like the individual data lines. However, it is not possible to set more than one colour - so color=c("black", "blue") does not work.

I've already tried scale_colour_manual as explained here, but this will change the colours of the individual data lines, leaving the mean lines unaffected.

Any suggestion how to solve this? Code and data here.

1

1 Answers

6
votes

You need to create different values for the mapping to color:

ggplot(data=iris, 
       aes(x=Sepal.Length, y=Sepal.Width, color=Species)) + 
  geom_line() + geom_point() + theme_bw() +
  stat_summary(fun.y=mean, geom="line", size=1.5,
               linetype="dotted", aes(color=paste("mean", Species)))

resulting plot

You can then use scale_color_manual to get specific colors.