Search code examples
rggplot2boxplotoutliers

Colouring extreme outliers in a boxplot using ggplot2


I have used ggplot2 to create a boxplot. I want the 'extreme' outliers (identified by the is_extreme function) to be in a different colour to the other outliers.

Here is my code:

ggplot(Mooney_data) + geom_boxplot(aes(x=Phase, y=RT), outlier.colour='red')+geom_point(data=Mooney_data[is_extreme(Mooney_data$RT),], aes(x= Phase, y=RT, col=factor(is_extreme(RT))))+ theme(legend.position='none')+mytheme

This gives me the extreme outliers in blue. However, I don't understand why the rest of the outliers are in two different shades of red? How can I correct this?

Boxplots with extreme outliers coloured in blue


Solution

  • You're getting two different shades of red because the default color scheme for ggplot with two factor levels is hue_pal()(2), the first of which is the shade of red you are seeing in addition to the 'red' that you have specified for outliers.

    library(scales)
    hue_pal()(2)
    [1] "#F8766D" "#00BFC4"
    

    If you want blue extreme outliers, put the col argument outside of the geom_point aesthetic:

    geom_point(data=Mooney_data[is_extreme(Mooney_data$RT),], 
    aes(x= Phase, y=RT), 
    col='blue'))  # <--- here