How to specify RGB colours and transparency in R plots

R handles colours like “red” or “blue” out of the box, but what if we want more precise colours? Enter rgb():

Here’s some simple code to illustrate how to use rgb in R: Let’s just draw 10 circles in pure red (red=1=100%, green=0%, blue=0%).

plot(1:10, rep(1,10), col=rgb(1, 0, 0, 0.5), pch=16)

The additional number here (0.5) is the alpha value to indicate transparency. 0.5 is 50% transparent.

Let’s add blue points:
points((1:10)+0.05, rep(1,10), col=rgb(0, 0, 1, 0.5), pch=16)

For intermediary colours, we need to specify the R, G, and B values as fractions: the division by 255 is not typically included on colour palettes.

rgb(68/255, 119/255, 170/255, 0.5)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: