R: Using RColorBrewer to colour your figures in R
RColorBrewer is an R packages that uses the work from http://colorbrewer2.org/ to help you choose sensible colour schemes for figures in R. For example if you are making a boxplot with eight boxes, what colours would you use, or if you are drawing six lines on an x-y plot what colours would you use so you can easily distinguish the colours and look them up on a key? RColorBrewer help you to do this.
Below is some example R code that generates a few plots, coloured by RColorBrewer.
The colours are split into three group, sequential, diverging, and qualitative.
This will generate 100 colours based on the 9 from the ‘Blues’ palette. See image below for a contrast.
Below is some example R code that generates a few plots, coloured by RColorBrewer.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### Load the package or install if not present | |
if (!require("RColorBrewer")) { | |
install.packages("RColorBrewer") | |
library(RColorBrewer) | |
} | |
### Set the display a 2 by 2 grid | |
par(mfrow=c(2,2)) | |
### Show all the colour schemes available | |
display.brewer.all() | |
### Generate random data matrix | |
rand.data <- replicate(8,rnorm(100,100,sd=1.5)) | |
### Draw a box plot, with each box coloured by the 'Set3' palette | |
boxplot(rand.data,col=brewer.pal(8,"Set3")) | |
### Draw plot of counts coloured by the 'Set3' pallatte | |
br.range <- seq(min(rand.data),max(rand.data),length.out=10) | |
results <- sapply(1:ncol(rand.data),function(x) hist(rand.data[,x],plot=F,br=br.range)$counts) | |
plot(x=br.range,ylim=range(results),type="n",ylab="Counts") | |
cols <- brewer.pal(8,"Set3") | |
lapply(1:ncol(results),function(x) lines(results[,x],col=cols[x],lwd=3)) | |
### Draw a pie chart | |
table.data <- table(round(rand.data)) | |
cols <- colorRampPalette(brewer.pal(8,"Dark2"))(length(table.data)) | |
pie(table.data,col=cols) |
The colours are split into three group, sequential, diverging, and qualitative.
- Sequential – Light colours for low data, dark for high data
- Diverging – Light colours for mid-range data, low and high contrasting dark colours
- Qualitative – Colours designed to give maximum visual difference between classes
The main function is brewer.pal, which you simply give the number of colours you want, and the name of the palette, which you can choose from running display.brewer.all()
There are limits on the number of colours you can get, but if you want to extend the Sequential or Diverging groups you can do so with the colorRampPalatte command, for example :
There are limits on the number of colours you can get, but if you want to extend the Sequential or Diverging groups you can do so with the colorRampPalatte command, for example :
colorRampPalette(brewer.pal(9,”Blues”))(100)
This will generate 100 colours based on the 9 from the ‘Blues’ palette. See image below for a contrast.
From compBiomeBlog |