Search code examples
rggplot2colorsscale

ggplot2: change color of one point with corresponding error bar


I generated the following plot:

enter image description here

and I'd like to change the color of the ref point + errorbar. What could be an easy way?

This is my code:

ggplot(df,aes(x = chan,y = mean)) + 
    geom_errorbar(aes(ymin = mean - sd, ymax = mean + sd)) +
    geom_point() + 
    theme_bw() +
    ylab("accuracy") + 
    xlab("permutation channel") + 
    scale_x_continuous(breaks = c(1:11), labels = c(1:10, "ref"))

Thank you!

EDIT:

structure(list(chan = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11), mean = c(0.951002180576324, 
0.954477310180664, 0.954147934913635, 0.955366730690002, 0.945781230926514, 
0.95714545249939, 0.95810067653656, 0.951051592826843, 0.947444677352905, 
0.946522414684296, 0.9573002576828), sd = c(0.00134088819225075, 
0.00426092161364007, 0.00461019843766268, 0.00517772940490045, 
0.000785548183287362, 0.00274702164693839, 0.004252234407014, 
0.00353576303790188, 0.00276008055853283, 0.00427941769453181, 
0.00225555534939029)), row.names = c(NA, -11L), class = "data.frame")

Solution

  • An alternative to adding extra layers is to map chan == 11 to the color aesthetic and add scale_color_manual. This involves a bit less duplication of code.

    ggplot(df,aes(x = chan, y = mean, color = chan == 11)) + 
      geom_errorbar(aes(ymin = mean - sd, ymax = mean + sd)) +
      geom_point() + 
      xlab("permutation channel") + 
      scale_x_continuous('accuracy', breaks = c(1:11), labels = c(1:10, "ref")) +
      scale_color_manual(values = c('black', 'red'), guide = 'none') +
      theme_bw()
    

    enter image description here