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.
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.
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.
Specifically, you will be able to:
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.
To explore the different color palettes in the package, use the “display.brewer.all” function.
If you visit the ColorBrewer website, you’ll see that in this package, we have 3 types of color palettes: sequential, diverging, and qualitative.
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.
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.
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")
# You can even merge 2 arguments/conditions together
display.brewer.all(colorblindFriendly = TRUE, type = "qual")
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:
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.
## [1] "#9E0142" "#D53E4F" "#F46D43" "#FDAE61" "#FEE08B" "#E6F598" "#ABDDA4"
## [8] "#66C2A5" "#3288BD" "#5E4FA2"
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.
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().
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.
Just as simple as this, and you have applied your color palette to your plot!
Learn more about the package with the following:
RColorBrewer CRAN Pdf: Click here to access it.
RColorBrewer Archive: Click here to access it.
R Graph Gallery: Click here to access it.
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.
This code through references and cites the following sources: