Member-only story
A Guide to using Hexadecimals for Color in R
Hexadecimals allow for specificity and flexibility.

In R, hexadecimal (hex) color codes are commonly used to specify colors in graphics and plots. Hexadecimal codes represent colors as a combination of red, green, and blue (RGB) values, where each color is represented by a two-digit hexadecimal number ranging from 00 to FF (0 to 255 in decimal).
Stated another way, we define a color as a 6 hexadecimal digit number of the form #RRGGBB
. Where the RR
is for red, GG
for green and BB
for blue and value ranges from 00
to FF
.
For example, #FF0000
would be red and #00FF00
would be green similarly, #FFFFFF
would be white and #000000
would be black. The color code #0000FF
represents pure blue, while##FF0000
represents pure red. Here are two examples:
temp <- c(5,7,6,4,8)
barplot(temp, col="#c00000", main="#c00000") # this is the first plot
barplot(temp, col="#AA4371", main="#AA4371") # this is the second plot


We can also color each bar of the barplot with a different color by providing a vector of colors made up of hexadecimals.
If the number of colors provided is less than the number of bars, the color vector is recycled.
We can see this in the following examples:
barplot(temp, col=c("#FF0099","#CCFF00","#33FF00",
"#00FF66","#FF9900"), main="With 5 colors") # this is the first plot
barplot(temp, col=c("#FF99FF","#0066FF","#00FF4D"), main="With 3 colors")
# this is the second plot

