Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

I often find myself coming back to this answer I gave on Stack Overflow in 2014. It shows how to colour a plot based on an independent continuous variable using the base graphics package. In the answer I gave it’s about colouring boxplots, but it equally applies to adding colour to a scatter plot. I’ve decided to elaborate here, as much for my personal use as anyone else’s!

You’ll note that I set pal twice, the first time is if you want to define the colours yourself and the second for use with the RColorBrewer package. If you’ve not come across it before it’s an excellent resource for choosing plot colours, here’s a map implementation: http://colorbrewer2.org, and you can view the colours here.

# Make some dummy data
df = data.frame(Year=1980:2010, Flow=rnorm(31, mean=150, sd=4),
Temp=rnorm(31, mean=0, sd=2))

# Optionally set colours using RColorBrewer
library(RColorBrewer)
cols = brewer.pal(4, "Blues")

# Define colour pallete
pal = colorRampPalette(c("blue", "red"))
# Use the following line with RColorBrewer
pal = colorRampPalette(cols)

# Rank variable for colour assignment
df$order = findInterval(df$Temp, sort(df$Temp)) # Make plot plot(Flow ~ Year, df, pch=19, col=pal(nrow(df))[df$order])
legend("topright", col=pal(2), pch=19,
legend=c(round(range(df\$Temp), 1)))


Here are the resulting plots:

Of course, you could also accomplish this using the ggplot2 package.  