Color palettes in R

In this post, we will see how to use different R color palettes in base R plots such as the barplot function.

Color palettes from base R

First, we can choose among the color palettes available in base R: rainbow, cm.colors, heat.colors, topo.colors, and terrain.colors. There is also the palette function which is the default choice in base R plots but which only contains 8 different colors.

Each palette produces a list of color names which are then used by the plotting function. For instance, the launch of rainbow(8) returns the following color names: c(“#FF0000”, “#FFBF00”, “#80FF00”, “#00FF40”, “#00FFFF”, “#0040FF”, “#8000FF”, “#FF00BF”). Here are barplots colored with each palette:

n = 8
barplot(rep(1, n), col = 1:n)) # same as: barplot(rep(1, n), col = palette()) 
barplot(rep(1, n), col = rainbow(n))
barplot(rep(1, n), col = cm.colors(n))
barplot(rep(1, n), col = heat.colors(n))
barplot(rep(1, n), col = topo.colors(n))
barplot(rep(1, n), col = terrain.colors(n))
barplots with color palettes from base R: palette, rainbow, cm.colors, heat.colors, topo.colors, terrain.colors

Color palettes from viridisLite

Hopefully, some R packages such as viridisLite offer prettier color palettes. For example, the viridis palette from this package is very popular and allows a good representation of the data. Below are the eight palettes available in viridisLite:

library(viridisLite)

n = 8
barplot(rep(1, n), col = rocket(n))
barplot(rep(1, n), col = mako(n))
barplot(rep(1, n), col = magma(n))
barplot(rep(1, n), col = viridis(n))
barplot(rep(1, n), col = inferno(n))
barplot(rep(1, n), col = cividis(n))
barplot(rep(1, n), col = plasma(n))
barplot(rep(1, n), col = turbo(n))
barplots with color palettes from viridisLite: rocket, mako, magma, viridis, inferno, cividis, plasma, turbo

Color palettes from RColorBrewer

Furthermore, the RColorBrewer package contains 35 distinct color palettes. They can be divided into 18 sequential, 8 qualitative, and 9 diverging palettes. Unfortunately, they are not continuous, meaning that they all have a minimum and a maximum number of possible values.

1) Sequential color palettes (from 3 to 9 values)

library(RColorBrewer)

n = 8
barplot(rep(1, n), col = brewer.pal(n, name = "BuPu"))
barplot(rep(1, n), col = brewer.pal(n, name = "Purples"))
barplot(rep(1, n), col = brewer.pal(n, name = "PuRd"))
barplot(rep(1, n), col = brewer.pal(n, name = "RdPu"))
barplot(rep(1, n), col = brewer.pal(n, name = "Reds"))
barplot(rep(1, n), col = brewer.pal(n, name = "OrRd"))
barplot(rep(1, n), col = brewer.pal(n, name = "Oranges"))
barplot(rep(1, n), col = brewer.pal(n, name = "YlOrRd"))
barplot(rep(1, n), col = brewer.pal(n, name = "YlOrBr"))
barplots with sequential color palettes from RColorBrewer
library(RColorBrewer)

n = 8
barplot(rep(1, n), col = brewer.pal(n, name = "PuBu"))
barplot(rep(1, n), col = brewer.pal(n, name = "PuBuGn"))
barplot(rep(1, n), col = brewer.pal(n, name = "Blues"))
barplot(rep(1, n), col = brewer.pal(n, name = "GnBu"))
barplot(rep(1, n), col = brewer.pal(n, name = "BuGn"))
barplot(rep(1, n), col = brewer.pal(n, name = "YlGnBu"))
barplot(rep(1, n), col = brewer.pal(n, name = "Greens"))
barplot(rep(1, n), col = brewer.pal(n, name = "YlGn"))
barplot(rep(1, n), col = brewer.pal(n, name = "Greys"))
barplots with sequential color palettes from RColorBrewer

2) Qualitative color palettes (from 3 to 8, 9 or 12 values)

library(RColorBrewer)

n = 8
barplot(rep(1, n), col = brewer.pal(n, name = "Accent")) # up to 8 values
barplot(rep(1, n), col = brewer.pal(n, name = "Dark2")) # up to 8 values
barplot(rep(1, n), col = brewer.pal(n, name = "Paired")) # up to 12 values
barplot(rep(1, n), col = brewer.pal(n, name = "Set1")) # up to 9 values
barplot(rep(1, n), col = brewer.pal(n, name = "Set2")) # up to 8 values
barplot(rep(1, n), col = brewer.pal(n, name = "Set3")) # up to 12 values
barplot(rep(1, n), col = brewer.pal(n, name = "Pastel1")) # up to 9 values
barplot(rep(1, n), col = brewer.pal(n, name = "Pastel2")) # up to 8 values
barplots with qualitative color palettes from RColorBrewer

3) Diverging color palettes (from 3 to 11 values)

library(RColorBrewer)

n = 8
barplot(rep(1, n), col = brewer.pal(n, name = "BrBG"))
barplot(rep(1, n), col = brewer.pal(n, name = "PiYG"))
barplot(rep(1, n), col = brewer.pal(n, name = "PuOr"))
barplot(rep(1, n), col = brewer.pal(n, name = "PRGn"))
barplot(rep(1, n), col = brewer.pal(n, name = "RdBu"))
barplot(rep(1, n), col = brewer.pal(n, name = "RdGy"))
barplot(rep(1, n), col = brewer.pal(n, name = "RdYlBu"))
barplot(rep(1, n), col = brewer.pal(n, name = "RdYlGn"))
barplot(rep(1, n), col = brewer.pal(n, name = "Spectral"))

Color palette emulated from ggplot2

Finally, if you like the default colors used in ggplot2, they can be emulated as explained in this discussion. Indeed, you can check in this post that I specified which variables must be colored but not which colors must be used. So here is the code to use ggplot2 colors in a base R plot:

gg_color_hue <- function(n) {
  hues = seq(15, 375, length = n + 1)
  hcl(h = hues, l = 65, c = 100)[1:n]
}

for (n in c(2, 4, 8, 16, 32)){
  barplot(rep(1, n), col = gg_color_hue(n))
}
barplots with color palette emulated from ggplot2

Conclusion

To sum up, it is really easy to improve the colors in base R plots with packages such as viridisLite and RColorBrewer. Which palette do you use the most?

Related posts

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply