Introduction

This Code Through explores the RColorBrewer package, which was published in December, 2014, by Erich Neuwirth. This R package is used to create beautiful colorful graphics, especially nice looking color palettes for thematic maps, as it provides you with ready-to-use color palettes that you can choose from. We are going to explore the different color palettes that the RColorBrewer package offers to us.


Overview

Specifically, we’ll explain and demonstrate how to use these different color palettes on ggplot2 scatter plots, from installing the package to applying a color palette on an existing plot.


Why You Should Care

This topic is valuable because it introduces you to a new way of coloring graphics which is really important, and it can be used to attract attention, organize content, highlight elements and help your graphic look aesthetically pleasing.


Learning Objectives

Specifically, you will be able to:

  • Install and load RcolorBrewer package and explore RColorBrewer palettes.
  • Visualize and return the hexadecimal color code of a specific brewer palette.
  • Change the ggplot colors using a color palette from RColorBrewer.



Installing and Loading the package

Just like any R package, to install the Rcolorbrewer package, we are going to use the install.packages() function in our R script and then run it in the console or type it directly in the console. Then to load the package, we are going to use function library()

install.packages("RColorBrewer")  #Installing the RcolorBrewer package

library(RColorBrewer)             #Loading the RcolorBrewer package

By this, you’ll have had installed the package and loaded it into your Rscript successfully.

Exploring RColorBrewer palettes

To explore the different color palettes in the package, use the “display.brewer.all” function.

display.brewer.all()  # The result of this is illustrated in the plot below

If you visit the ColorBrewer website, you’ll see that in this package, we have 3 types of color palettes: sequential, diverging, and qualitative.

  1. Sequential palettes - these are perfect for ordered data. It has a kind of a lightness gradient, with light colors for low data values to dark colors for high data values. Each palette is available in variations from 3 different values up to 9 different values. Some of the palettes names are Blues, BuGn, BuPu, GnBu, and Greens.

  2. Diverging palettes - these palettes help put equal emphasis on mid-range critical values (emphasized with light colors), as well as extremes at both ends (dark colors with contrasting hues). Some of the palettes names are BrBG, PiYG, PRGn, and PuOr.

  3. Qualitative palettes these palettes are preferred for representing nominal or categorical data. They, however, do not imply magnitude differences between groups.Hues there create the primary visual differences between classes. Some of the palettes names are Accent, Dark2, Paired, and Pastel1.


To get more familiar with the names of the different palettes, make sure you read this pdf from CRAN.

What’s more brilliant,

We can specify exactly the palettes what we want to display by using different arguments.

# This will only display sequential palettes as illustrated in the plot below
display.brewer.all(type = "seq")  

# This will only display diverging palettes as illustrated in the plot below
display.brewer.all(type = "div")  

# This will only display qualitative palettes as illustrated in the plot below
display.brewer.all(type = "qual")  

# This displays colorblind-friendly brewer palettes
display.brewer.all(colorblindFriendly = TRUE)

# You can even merge 2 arguments/conditions together
display.brewer.all(colorblindFriendly = TRUE, type = "qual")

Visualizing a specific brewer palette

We do this with function display.brewer.pal(), but before we get into this, there are a couple of function arguments that you should know:

  1. n: Number of different colors in the palette, minimum 3.
  2. name: A palette name from the lists above like RdBu for example.

You code should look like this display.brewer.pal(n, name).

# We can view a specific RColorBrewer palette by simply specifying its name
display.brewer.pal(n = 10, name = 'Spectral')

Sometimes we need to know the exact color code for a shade of color that we like, thus returning the hexadecimal color code in this case is very important.

We do this with function brewer.pal(). Your code should look like this brewer.pal(n, name). You’ll find an example below.

brewer.pal(n = 10, name = "Spectral")
##  [1] "#9E0142" "#D53E4F" "#F46D43" "#FDAE61" "#FEE08B" "#E6F598" "#ABDDA4"
##  [8] "#66C2A5" "#3288BD" "#5E4FA2"

Changing the ggplot colors using a color palette from RColorBrewer

We chose the iris dataset since it has a variable with three values (Species), the following code chunk was copied from a website since it’s not our main focus here. Make sure you visit this page to learn more about scatter plots.

sp <- ggplot(iris, aes(Sepal.Length, Sepal.Width))+
  geom_point(aes(color = Species))
sp

To change the previous iris ggplot using one of the RColorBrewer palettes, you simply have to use either function scale_fill_brewer() or function scale_color_brewer().

  1. scale_fill_brewer() is used for box plot, bar plot, violin plot, and dot plot.
  2. scale_color_brewer() is used for lines and points.

Because we are using a scatter plot, we will be using function scale_color_brewer()

All you have to do is adding the following code chunk: sp + scale_color_brewer(palette = “palette name”). Below is an example illustrating this.

sp + scale_color_brewer(palette = "RdYlGn")

Just as simple as this, and you have applied your color palette to your plot!


Further Resources

Learn more about the package with the following:

Also note: this package is based on the colorbrewer2.org website, so make sure you check their website first thing to get familiar with the colorbrewer’s palettes.


Works Cited

This code through references and cites the following sources: