Introduction and Motivation


The purpose of this post is to highlight a very cool interactive way to help you choose a color palette when you’re working on a visualization. I like to compare this app to going to the paint store to find swatches of colors you can take home with you when when you’re thinking about repainting your bedroom or kitchen. It is super useful to have examples of the different color options in front of the blank wall at home and in our case here, equally important to have color options readily visible when you’re working on an R visualization!

palette_explorer() is like bringing swatches home with you so you can find the best color fit! (It’s also just as fun) :) Actually, I also found out that these palettes are Cindy Brewer’s and she has created an online app to help you choose colors. What’s nice about palette_explorer() is that you can do all your choosing in R as you create your visualization. This blog post will discuss how to use this interactive app, what each element of the app does, and what it means for your data visualization process in general.

My motivation for writing about this topic particularly is that until recently, I did not know about this cool little app. It came in handy when I was putting together some basic bar plots as a secondary analysis of local business data. The main analysis was a time series projection of growth, but there was some useful rating data I placed in bar charts for the owners review, that I didn’t want to exclude. When I presented my findings, it was much nicer to use a palette_explorer() swatch rather than creating my own. I made the bar charts easier on his eyes, while not taking precious time away from the main analysis. Next time you want to add some color to your plots and charts, it will free up some of the time it would’ve otherwise taken you to do so!

What’s better than saving time? (I can answer that)… nothing is.


Background


If I’m working on a plot in R I usually stick to one color or maybe two colors for different aspects of my plot (if I use color at all). If the visualization is for my own purposes I tend to leave it plain and simple. But what if I wanted to spice things up? I could maybe Google search for a palette online. This is great but it’s hard to remember which colors each palette contains. I know “BuPu” is a purple-ish color scheme because it’s my favorite but other than that I only know “Spectral”, and “Set1” off hand.

That’s boring!

Enter the palette_explorer() tool given by the “tmaptools” package written by Martijn Tennekes of the Netherlands. Martijn developed these tools to help make spatial data analysis in R more fluid. Consequently, function tools in this package also helps make creating other types of visualizations more fluid. Using palette_explorer() cuts the time it takes to find an appealing ColorBrewer palette for your plot or graph in half! Woo hoo!


Examples


Here’s a code example of using ColorBrewer to give color to a very simple ggplot bar plot. (I chose this plot to keep things simple; I realize it’s not a great example for using color in a visualization)

##Library call the necessary packages
library(RColorBrewer)
library(ggplot2)

##Create the data frame from which your ggplot will be made
df.dummy_data <- data.frame(category_var = c("A","B","C","D","E"), 
                                                 numeric_var = c(5, 2, 9, 4, 5))

## Make Letter for sidebar area of bar plot
Letter <- df.dummy_data$category_var

##Make your ggplot
dummy_plot <- ggplot(data=df.dummy_data, 
                    aes(x=category_var, y=numeric_var, fill = Letter)) + 
                    geom_bar(stat="identity") + 
                    xlab("Letter (A-E)") + 
                    ylab("Number (0-9)") + 
                    ggtitle("Dummy Bar Plot Example for ColorBrewer Palette") + 
                    scale_fill_brewer(palette = "BuPu")

dummy_plot

This is great! Its colorful and nice to look at. However we only know the palette “BuPu” because we looked it up one day and happened to remember it.

We could use the same plot and try a different palette that we happen to remember. All we need to do is change the call to scale_fill_brewer() to scale_fill_brewer(palette = "Spectral").

Also pretty cool!

But as I mentioned, wouldn’t it be awesome if we could just pick from a descriptive list without having to pull up a search bar and scour the internet for something useful?

Yes. It would be awesome.

This example goes there:

## To use this interactive app you need to install the tmaptools package
## install.packages(tmaptools)

## Same as the last two examples you need to make a few library calls. This 
#example calls tmaptools as well

library(RColorBrewer)
library(ggplot2)
library(tmaptools)
## Warning: package 'tmaptools' was built under R version 3.4.4
library(rsconnect)

## palette_explorer()

## ^ This is commented out and in place of embedding a shiny app into this html 
#document, I've taken a screenshot of the app shown below.

Visuals


Here is a look at what pops up when you use palette_explorer():