Colouring a plot using a continuous variable in R

September 29, 2015
By

(This article was first published on R – scottishsnow, and kindly contributed to R-bloggers)

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 on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Sponsors

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)