Using Color and Themes in ggplot2


Gain familiarity with the way that color palettes and aesthetic themes are specified in ggplot2, so that you can use both in your own visualizations.


The examples will use the storms dataset which is included in the dplyr package (both dplyr and ggplot2 are loaded by tidyverse, so it suffices to do library(tidyverse)). The data comes from the NOAA Atlantic hurricane database, and includes positions and attributes of 198 tropical storms measured at six hour intervals during each storm’s lifetime.

  1. Load the storms data, and examine the documentation using the ?datasetname syntax to see the definitions of the variables.


  1. Make a box plot depicting the distribution of wind speed grouped by the type of storm (tropical depression, tropical storm, or hurricane) and save it to a variable called wind_boxplot.

Side note: reordering a categorical variable

You might notice that in the boxplot you get, the storm types are ordered alphabetically. In this case the types have a natural ordering based on their intensity: tropical depressions are the least severe, and hurricanes the most. Our plot will be more natural if we respect this natural ordering. We can do this as follows:


Adding color

We can add some color to the plot by adding a redundant mapping: in addition to mapping status to the x dimension, we’ll also map it to the fill feature (the interior color of the boxplots).


Specifying colors manually

The default color palette looks pretty nice, but just for fun let’s see how we could change it.

One option is to directly specify the colors we want R to use, using hexadecimal codes for the colors’ RGB values. The colors in the following example come from one of my personal favorite palettes (you might recognize it from my website, for example), the Solarized palette by Ethan Schoonover. In each code, the first two characters represent the red channel (on a 0-255 scale, encoded in base 16), the next two represent the green channel, and the last two represent the blue channel.


Using a predefined palette

Manually specifying colors allows very precise control, but it’s tedious, time consuming, and brittle. It is generally better and easier to use predefined color palettes instead.

A fairly large and diverse collection of color palettes was developed by Cynthia Brewer (link) and is available in the RColorBrewer package.

After loading the package, we can view the included palettes with display.brewer.all()