# Using R to Create Custom Color Palettes for Tableau

Have you ever wanted to define custom color palettes in Tableau, but didn’t know how? In this post, I’m going to walk through how we can use R to programmatically generate custom palettes in Tableau. Creating custom color palettes for Tableau has never been easier!

This is going to be a short post, with just a little bit of R code.

At the end of the post, you’ll see how to use R to generate custom color palettes to add to Tableau. We’ll show how to add palettes from the viridis color palette and ColorBrewer to Tableau.

## Defining Custom Color Palettes

Tableau already has a pretty good tutorial and this tutorial is pretty good too, but I thought I’d share some R code that helps to make it easier to define custom palettes.

The basics of defining custom palettes in Tableau is that you have to modify the Preferences.tps file that comes with Tableau. This file can be found in your My Tableau Repository. It’s an XML file, which makes it pretty easy to hack around in the text editor of your choice (I prefer Sublime Text).

If we wanted to define a custom palette, based on the three color Set1 palette from ColorBrewer, we would just add this to our Preferences.tps file:

<color-palette name="Set1 3 Color Qual Palette" type="regular">
<color>#E41A1C</color>
<color>#377EB8</color>
<color>#4DAF4A</color>
</color-palette>

In it, we defined the name, the type (regular, ordered-diverging, or ordered-sequential), and the colors (HEX codes).

If you wanted to hand-edit this file, it might be tedious and you’d need to do a lot of copying-and-pasting.

So, why not write a quick function in R to generate this?

Or, maybe you’d just like a pre-filled Preferences.tps file with many useful palettes added already. If so, check out my GitHub repository which has a fairly complete Preferences.tps file.

## The create_tableau_palette function

The following function takes three arguments:

• palette_name: this is what you want the name to be in your file. In the example above, it was Set1 3 Color Qual Palette. Make sure you name it something descriptive enough to be found easily in Tableau.
• palette_colors: this is a character vector of colors which will be added to the palette. You should use HEX codes (e.g. "#E41A1C", "#377EB8")
• palette_type: this is one of the three palette types described above. In the previous example, it was regular.

The function will then print the resulting color palette to the console, so you can copy and paste the results. It uses the cat function, so it only prints stuff to the console, it isn’t necessary to store the result in a variable.

create_tableau_palette <- function(palette_name,
palette_colors,
palette_type) {
# check palette type
p_type = match.arg(palette_type,
choices = c('ordered-diverging',
'ordered-sequential',
'regular'))
# starting line
line_start <- paste0('<color-palette name="',
palette_name,
'" type="',
p_type,
'">\n')
# define colors
colors <- paste0('<color>',
palette_colors,
'</color>\n')
# ending line
line_end <- "</color-palette>\n"
# push together
cat(paste0(c(line_start, colors, line_end)))
}

Here’s an example:

# character vector of first four Set2 color values
brewer_4 <- RColorBrewer::brewer.pal(4, 'Set2')
# use the function
create_tableau_palette(palette_name = "Color Brewer Set2 4",
palette_colors = brewer_4,
palette_type = 'regular')
## <color-palette name="Color Brewer Set2 4" type="regular">
##  <color>#66C2A5</color>
##  <color>#FC8D62</color>
##  <color>#8DA0CB</color>
##  <color>#E78AC3</color>
##  </color-palette>

You could take the result above (remove the ## that results from printing) and copy and paste it into the Preferences.tps file.

Of course, we could loop through different specifications to create many custom palettes rather quickly.

## Generating Viridis Palettes for Tableau

Let’s use this function to generate custom Tableau color palettes for the popular viridis palette.

We’re using the purrr package to do our “looping”.

# need to store result for better printing, result is just a list of NULL
x<-purrr::map(4:7,
~create_tableau_palette(palette_name = paste('Viridis', .x),
palette_colors = viridis::viridis(.x),
palette_type = 'ordered-sequential'))
## <color-palette name="Viridis 4" type="ordered-sequential">
##  <color>#440154FF</color>
##  <color>#31688EFF</color>
##  <color>#35B779FF</color>
##  <color>#FDE725FF</color>
##  </color-palette>
## <color-palette name="Viridis 5" type="ordered-sequential">
##  <color>#440154FF</color>
##  <color>#3B528BFF</color>
##  <color>#21908CFF</color>
##  <color>#5DC863FF</color>
##  <color>#FDE725FF</color>
##  </color-palette>
## <color-palette name="Viridis 6" type="ordered-sequential">
##  <color>#440154FF</color>
##  <color>#414487FF</color>
##  <color>#2A788EFF</color>
##  <color>#22A884FF</color>
##  <color>#FDE725FF</color>
##  </color-palette>
## <color-palette name="Viridis 7" type="ordered-sequential">
##  <color>#440154FF</color>
##  <color>#443A83FF</color>
##  <color>#31688EFF</color>
##  <color>#21908CFF</color>
##  <color>#35B779FF</color>
##  <color>#8FD744FF</color>
##  <color>#FDE725FF</color>
##  </color-palette>

And then you can copy and paste that right into your Preferences.tps file! You’ll need to remove those ## symbols, but that shouldn’t be an issue if you’re using this function in your own R session. After you’ve added that to your file, restart Tableau, and then you should find the new palettes in your color choices.

## Wrapping Up

This was a short post illustrating one way to use R to generate custom palettes for Tableau. I really like Tableau as a way to build interactive dashboards, but I have found the default color palettes to be somewhat lacking (or maybe I just have high color palette standards). Hopefully this post will show you how easy it is to add new palettes to Tableau without having to do too much tedious copying-and-pasting.