This short tutorial gives an idea of how to use the RColorBrewer package to make plots with nice colours in R. ### Step 1: Install and load the R Color Brewer package Website: http://colorbrewer2.org/

# uncomment this line if you need to install the package..
# install.packages('RColorBrewer')
library('RColorBrewer')

To see what colour schemes are available, we can plot a graphic with all of the brewer palettes

display.brewer.all()

Example 1: Categorical Data

Here we will pick colours for a scatter plot with categorical data

# This palette looks nice. How is it with three colours?
display.brewer.pal(3, 'Dark2')

# Nice! Let's save this palette to a variable so that we can use it
scatter_cols <- brewer.pal(3, 'Dark2')

# Test with an example scatter plot using dummy data
attach(mtcars) # example data about cars
cyl # categorical data - all 4s, 6s and 8s
##  [1] 6 6 4 6 8 6 8 4 4 6 6 8 8 8 8 8 8 4 4 4 4 8 8 8 8 4 4 4 8 6 8 4
# Convert categories to colours
cyl[cyl == 4] <- scatter_cols[1]
cyl[cyl == 6] <- scatter_cols[2]
cyl[cyl == 8] <- scatter_cols[3]
cyl # Check that it looks right
##  [1] "#D95F02" "#D95F02" "#1B9E77" "#D95F02" "#7570B3" "#D95F02" "#7570B3"
##  [8] "#1B9E77" "#1B9E77" "#D95F02" "#D95F02" "#7570B3" "#7570B3" "#7570B3"
## [15] "#7570B3" "#7570B3" "#7570B3" "#1B9E77" "#1B9E77" "#1B9E77" "#1B9E77"
## [22] "#7570B3" "#7570B3" "#7570B3" "#7570B3" "#1B9E77" "#1B9E77" "#1B9E77"
## [29] "#7570B3" "#D95F02" "#7570B3" "#1B9E77"
plot(wt, mpg, main="Scatterplot Example", col = cyl, xlab="Car Weight ", ylab="Miles Per Gallon ", pch=19)

Example 1: Numerical Data

Here we use a gradient scheme for numerical data

# Nice divergent colour scheme - we want as many colours as possible
display.brewer.pal(11, 'PuOr')

numerical_cols <- brewer.pal(11, 'PuOr')

# Example data again - this is numerical rather than categorical though
drat
##  [1] 3.90 3.90 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 3.92 3.07 3.07 3.07
## [15] 2.93 3.00 3.23 4.08 4.93 4.22 3.70 2.76 3.15 3.73 3.08 4.08 4.43 3.77
## [29] 4.22 3.62 3.54 4.11
# 11 different colours isn't enough, lets make 500 so that it looks smooth
grad_cols <- colorRampPalette(numerical_cols)(500)

# Ok, figure out colour indexes from the data
# I'd normally do this in one or two lines but have split it up for clarity
drat.range <- range(drat) # gives min and max
drat.min <- drat.range[1]
drat.max <- drat.range[2]
drat.datarange <- drat.max - drat.min # span of the data
drat.proportions <- (drat - drat.min) / drat.datarange
drat.proportions <- drat.proportions * 500 #  as we have 500 colours
drat.indexes <- round(drat.proportions, digits = 0)

# Assign colours based on the indexes that we just worked out
plot_cols <- grad_cols[drat.indexes]
plot(wt, mpg, main="Scatterplot Example", col = plot_cols, xlab="Car Weight ", ylab="Miles Per Gallon ", pch=19)
points(wt, mpg, pch=1) # draw circles around the dots so that the white ones show

Ok, sorted! You can use colours like this for pretty much any plot and they nearly always look nicer than the R defaults..