×

Our next R course is at University of Queensland (Australia) from 18th Feburary 2020. Beginner, intermediate and advanced classes available.

seascape models

Making continuous colour palettes with RColorBrewer

The RColorBrewer package makes choosing colours for your graphs easy. If you follow the link on the help page for the handy brewer.pal function, you can get to a web app that lets you choose the type of palette (linear, diverging, qualitative), the number of colours and other options, like palettes that will be visable to people who are red-green colour blind (about 4% of the population).
This all extremely useful, but RColorBrewer palettes are generally limited to a maximum of 10 different colours. What if you want to have a continuous scale? Here I run through how to make a colour ramp from a Brewer palette.
Note though, that the designers of RColorBrewer limited the number of colours as a deliberate choice. The colour palettes are mainly designed for maps, and in general, people can't keep more than about 7 categories in their head at any one time. Hence, before you create your figure, think about how you want to represent on your figure. For instance, say you are plotting elevation. Choosing a few colours will emphasis zones in elevation, for instance, maybe you want to represent different vegetation zones that change with altitude. Whereas, a continuous palette will obviously represent the data as more of a gradient.

Colour Brewer categorical colours

library(RColorBrewer)
bluecols <- brewer.pal(9, 'Blues')

Which just makes a pie with nine different shades of blue. ### A colour ramp We can use the colorRampPalette() function to create a new function, here newcol(). newcol() takes a single integer as an argument, which outputs the colour ramp using our blue palette.

newcol <- colorRampPalette(bluecols)
ncols <- 100
bluecols2 <- newcol(ncols)#apply the function to get 100 colours

And that is it. We now have the RColorBrewer palette with 100 different shades of blue.



Contact: Chris Brown

Email Tweets Code on Github

home

Designed by Chris Brown. Source on Github