Colouring a plot using a continuous variable in R

[This article was first published on R – scottishsnow, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
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])
# Add a simple legend
legend("topright", col=pal(2), pch=19,
   legend=c(round(range(df$Temp), 1)))

Here are the resulting plots:

Adding colour to base graphics R plots.

Adding colour to base graphics R plots.

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


To leave a comment for the author, please follow the link and comment on their blog: R – scottishsnow.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)