Control ggplot2 boxplot colors

A boxplot summarizes the distribution of a continuous variable. Different color scales can be apply to it, and this post describes how to do so using the ggplot2 library. It is notably described how to highlight a specific group of interest.

Boxplot Section Boxplot pitfalls

General color customization

These for examples illustrate the most common color scales used in boxplot.

Note the use of RcolorBrewer and viridis to automatically generate nice color palette.

Highlighting a group

Highlighting the main message conveid by your chart is an important step in dataviz. If your story focuses on a specific group, you should highlight it in your boxplot.

To do so, first create a new column with mutate where you store the binary information: highlight ot not. Then just provide this column to the fill argument of ggplot2 and eventually custom the appearance of the highlighted group with scale_fill_manual and scale_alpha_manual.

# Libraries

# Work with the natively available mpg dataset
mpg %>% 
  # Add a column called 'type': do we want to highlight the group or not?
  mutate( type=ifelse(class=="subcompact","Highlighted","Normal")) %>%
  # Build the boxplot. In the 'fill' argument, give this column
  ggplot( aes(x=class, y=hwy, fill=type, alpha=type)) + 
    geom_boxplot() +
    scale_fill_manual(values=c("#69b3a2", "grey")) +
    scale_alpha_manual(values=c(1,0.1)) +
    theme_ipsum() +
    theme(legend.position = "none") +

Related chart types



This document is a work by Yan Holtz. Any feedback is highly encouraged. You can fill an issue on Github, drop me a message on Twitter, or send an email pasting with

Github Twitter