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

q <- ggplot(subset, aes(x = clarity, y = carat, color = cut)) +
  geom_point(size = 2.5,alpha = 0.75) 

q + theme_minimal()

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

q <- ggplot(subset, aes(x = clarity, y = carat, color = cut)) +
  geom_point(size = 2.5, alpha = 0.75) 
  • 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
q + scale_colour_brewer(palette = "Blues")

  • Diverging Palette: It has darker colors of contrasting hues on both the ends, and lighter color in the middle. For instance –Spectral, RdGy, PuOr
q + scale_colour_brewer(palette = "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
q + scale_colour_brewer(palette = "Pastel1")

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.

g1 <- ggplot(subset, aes(x = clarity, y = carat, color = cut)) +
  geom_point(size = 2.5,alpha = 0.75) 

27.4.1 ggthemes examples

g1 + theme_economist() + scale_colour_economist()

g1 + theme_igray() + scale_colour_tableau()

g1 + theme_wsj() + scale_color_wsj()

g1 + theme_igray() + scale_colour_colorblind()

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:

show_col(colorblind_pal()(6))

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.5.1 ggthemr examples

ggthemr("sky")

ggplot(subset, aes(x = clarity, y = carat, color = cut)) +
  geom_point(size = 2.5, alpha = 0.75)

ggthemr("flat")

ggplot(subset, aes(x = clarity, y = carat, color = cut)) +
  geom_point(size = 2.5, alpha = 0.75)

Interestingly, we can set more parameters to change the themes:

ggthemr("lilac", type = "outer", layout = "scientific", spacing = 2)

ggplot(subset, aes(x = clarity, y = carat, color = cut)) +
  geom_point(size = 2.5, alpha = 0.75)

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.1 ggsci for discrete data

# we need to remove the theme set previously if we don't want to use it anymore
ggthemr_reset()

g1 <- ggplot(subset, aes(x = clarity, y = carat, color = cut)) +
  geom_point(size = 2.5, alpha = 0.75)

g1 + scale_color_startrek()

g1 + scale_color_jama()

g1 + scale_color_locuszoom()

27.6.2 ggsci for continuous data

ggplot(diamonds, aes(carat, price)) +
  geom_hex(bins = 20, color = "red") +
  scale_fill_material("orange")

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

palette = pal_lancet("lanonc", alpha = 0.7)(9)
show_col(palette)

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






with