Search code examples
rggplot2colors

Changing colour for geom_line in ggplot2 independently from group colour


I have the following aggregated data with means and confidence intervals, computed using SummarySEwithin. I have used this to generate the plot below.

Data set

The code for the plot is as follows:

ggplot(df_forplot, aes(x=Phase, y=criterion, colour=Phase)) +
 geom_line(aes(x=Phase, y=criterion, group=1))+geom_point() +
  geom_errorbar(aes(ymin=criterion-ci, ymax= criterion+ci),width=.1) +
    scale_colour_manual(values= c("#3399FF", "#FF3366"))+
      theme(legend.position = "none")+ mytheme

I want to change the line colour to black. How do I achieve this?

Current Plot


Solution

  • As Carl points out above, if you want to change a feature (say, a color) in a plot layer to a fixed value (so it does not depend on data), you need to specify it outside of aes(...). I have added , colour="black" fragment in a second line of the code snippet below.

    Try this code:

    ggplot(df_forplot, aes(x=Phase, y=criterion, colour=Phase)) +
     geom_line(aes(x=Phase, y=criterion, group=1), colour="black")+geom_point() +
      geom_errorbar(aes(ymin=criterion-ci, ymax= criterion+ci),width=.1) +
        scale_colour_manual(values= c("#3399FF", "#FF3366"))+
          theme(legend.position = "none")+ mytheme