R colour packages

Themed colour palettes for R are really popular at the moment. The popularity of these project packages is unsurprising. It’s the perfect blend of the geekiness of pop culture and the nerdiness of coding. There are palettes for Game of Thrones, Wes Andersen movies, cities, national parks in the US, Studio Ghibli movies, birds & fish to name a few.

Unfortunately, there is a gap in the market because no-one has made a palette for Australia. Australia is a beautiful country. Just look at the Tourism Australia instagram account. And something has to be done about it.

Nicholas Wu, a fellow postdoc, asked me to make him a palette package that he could add hex codes to and use in graphs. I’ve never made an R package before so I said I would look into it. Turns out, the existing packages are quite simple and follow a similar structure.

I tested a basic package by creating colour palettes based on the Warramaba grasshoppers I studied in the PhD. You can find the package, called colRoz, on my GitHub. Here’s an example of a palette based on Warramaba ngadju:

Here, I describe how the package works so you can adapt an existing package.

A simple package to start with as a template is the wesanderson package. This package is well documented which should help with building a package for the first timer. Two options for starting a package from an existing package are to initiate a new package project in RStudio and copy the R scripts over, or to clone the package from GitHub. The instructions to do so are covered elsewhere.

Packages in R are a collection of functions or data, stored in R scripts. There are three R scripts in a colour package:

  1. A list of palettes
  2. A function to plot the palette
  3. A function to call the palette into the R environment for use

The list of palettes are pretty self-explanatory. They list the palette names and the hex codes of the palette.

The function to plot the palette is only for graphing. Information is taken about the number of colours to plot from the desired palette and the palette is plotted. There is a handy box and text for identifying the palette shown.

The main function is the palette generator. It acts as a housekeeping function to allow R to interpret the desired palette for plotting. The behaviour of this function depends on whether a discrete or continuous palette is desired and the number of colours.

If a discrete palette of 3 colours is desired, then the function will chose the subset of 3 colours to be included from the full option of colours in a palette. The chosen order of these colours is hard coded in the list of palette.

If a continuous palette is desired, then the function will use the function colorRampPalette in the grDevices package (included in R) to generate a gradient of colours between the first and last colour in the desired palette.

Before you build a package in R, you need to include documentation:

  • Description: this tells R the metadata about your package; the title, date, website, authors etc.
  • License: this tells people the legality of how they can use your code. Not all packages have it but it is a good habit to have
  • Readme: important if you want to share your package, like through GitHub, you can include instructions or worked examples

You can share your package online by hosting the code on GitHub or similar repository. One of the nice things about GitHub is that you can make a website about your package, which comes in handy for bigger and more complex packages.

I recommend themed palette packages as a learning exercise for building packages.

If you are going to build a colour palette package, there are a two things to keep in mind (that I didn’t here). First, aim to have colours that are distinctive enough to be differentiated under a continuous gradient or a discrete scale. Help your audience to read your graph as much as possible. Second, aim to have colours accessible for colour-blindness. The second goal may not be feasible for really niche packages based on images but is generally important in design.

Happy package building!

Leave a comment