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():

I think the “Dark2” palette looks interesting so I can recreate my barplot with it. I change scale_fill_brewer() again to use this palette.


Discussion


Now that you’ve seen palette_explorer in action, I’ll discuss what each part of the app does.

There are 3 main sections on the left hand side of the app. They are the categories for what type of palette shows on they right.

Each of these sections has a little subsection called ‘Number of Colors’ where you can use the Shiny slider to select the number of different colors you want to see in the palette selections to match your visualization. The code is displayed beneath the palette on the bottom right. You will notice that it changes depending on your selections.

Under both the ‘Sequential’ and ‘Diverging’ labels you will find a button to turn automatic contrast on or off as well as a slider which you can adjust when the automatic option is deselected. This is nice when you want to use softer hues for your plot.


Conclusions


Aside from this being a very convenient and easy-to-use tool, one aspect of palette_explorer() is the option to choose colors that very thoughtfully work around color blindness. You can select any of 3 different options aside from the option for ‘normal’ that will give you a palette based on whether you are wanting to make your visualizations readable for those with either Tritanopia (yellow-blue color blindness), Protanopia (red-green color blindness), or Deuteranopia (confusion of greens, reds, and yellows).

Considering that about 1 in 12 men and 1 in 200 women suffer some type of color blindness this is a fantastic feature to keep in mind if you ever have an audience you know may be color blind. Imagine the time and effort it might take to understand how to construct a palette to accommodate color blindness!

So the take home message here is that this is a great resource to use when you’d like to spice up your visualizations if you feel the urge, and it is a really great example/application of how a shiny app can be useful.


References