27 Themes and Palettes

This chapter originated as a community contribution created by ar3879

This page is a work in progress. We appreciate any input you may have. If you would like to help improve this page, consider contributing to our repo.

27.1 Overview

Our graphs have to be informative and attractive to the audience to get their attention. Themes and colors play an important role in making the graphs attractive.

This section covers how we can set different palettes and themes to suit the context and to make them look cool.

27.2 ggplot2 themes

In ggplot2, we do have a set of themes, which we can set. A brief description of them is as follows:

  • theme_gray(): signature ggplot2 theme
  • theme_bw(): dark on light ggplot2 theme
  • theme_linedraw(): uses black lines on white backgrounds only
  • theme_light(): similar to linedraw() but with grey lines as well
  • theme_dark(): lines on a dark background instead of light
  • theme_minimal(): no background annotations, minimal feel
  • theme_classic(): theme with no grid lines
  • theme_void(): empty theme with no elements

27.2.1 ggplot2 theme example

There are several other packages available that set the themes and colors in many ways. We will discuss 4 of them.

  1. RColorBrewer
  2. ggthemes
  3. ggthemr
  4. ggsci

27.3 RColorBrewer

Often, we find ourselves looking for the colors which make our graph look clear and cool.

RColorBrewer offers a number of palettes, which we can use based on the context of our graph. There are three categories of these palettes: Sequential, Diverging, and Qualitative

  • Sequential Palette: It represents the shade of the color from light to dark. It is usually used to represent interval data where low values can be shown with a light color and high values can be shown with a dark color. For instance –Blues, BuPu, YlGn, Reds, OrRd

  • Diverging Palette: It has darker colors of contrasting hues on both the ends, and lighter color in the middle. For instance –Spectral, RdGy, PuOr

  • Qualitative Palette: It is usually used when we want to highlight the differences in the classes (categorial variables). For instance –set1, set2, set3, pastel1, pastel2 , dark2

27.4 ggthemes

ggthemes provides additional geoms, scales, and themes to ggplot2. Some of them are really cool! We can change the theme and color of a graph based on the context.

27.4.1 ggthemes examples

If we would like to use these colors in the graphs, which may not support using ggthemes, we can use the scales package to know what colors were used for a given palette. For example:

27.5 ggthemr

ggthemr is used to set the theme of ggplot graphs. It has 17 different themes to change the way ggplot graphs look. Use of ggthemr is different from other other packages. We set the theme before using it.

27.6 ggsci

ggsci offers a number of palettes inspired by colors used in scientific journals, science fiction movies, and TV shows. For continous data, scale_fill_material(colname) is used, and for discrete data, scale_color_palname() or scale_fill_palname() are used.

27.6.2 ggsci for continuous data

We can also find out the color used, so that we can use them in some other graphs created in base R:

27.7 External Resources

  • RColorBrewer: Setting up Color Palettes in R
  • ggthemes: Github page containing more examples
  • ggthemr: Github Repository of the package
  • ggsci: Scientific Journal and Sci-Fi Themed Color Palettes for ggplot2