ggplot2: Qualitative Colour Palettes

2009 April 15

Every now and then I would like to change the colours of my plots. ggplot2 provides several different ways to accomplish this task, however choosing the one that matches my mood on that particular day is always relatively time-consuming. So I decided to make a post highlighting some of the available colour palette options. In this post I will focus on the colour schemes helping to depict categorical data, and will try to use various predetermined colour palettes as much as possible, as I find hard to distinguish between the intricacies of different color spaces including RGB, HSV, HLS, CIEXYZ, CIELUV, HCL (polar CIELUV), CIELAB and polar CIELAB.

Default formatting

First, load the necessary libraries and set up a dummy dataframe of 8 variables for plotting. As the main focus is on the colours I remove the legends, titles and ticks from the plots.

library(ggplot2)
library(colorspace)
df <- data.frame(x=letters[1:8])p <- qplot(data=df, x, 1, geom="bar", stat="identity", fill=x) +
opts(
legend.position = "none",
axis.title.x = theme_blank(),
axis.title.y = theme_blank(),
axis.text.x = theme_blank(),
axis.text.y = theme_blank(),
axis.ticks = theme_blank(),
title="Default colours")

colours-p1

Colorbrewer

ColorBrewer is an “online tool designed to help people select good color schemes for maps and other graphics”. ggplot2 supports the use of their colour schemes out-of-the-box.

brewerplot <- function (palette) {
p + scale_fill_brewer(palette = palette) + opts(title=palette)
}
brewerplot ("Set1")

colours-set1

brewerplot ("Set2")
colours-set2

brewerplot ("Set3")
colours-set3

brewerplot ("Accent")
colours-accent

brewerplot ("Dark2")
colours-dark2

brewerplot ("Paired")
colours-paired

brewerplot ("Pastel1")
colours-pastel1

brewerplot ("Pastel2")
colours-pastel2

Colorspace

R package colorspace includes several palettes based on HCL colours. The plots below are using the palettes for 4 categorical variables given in the colorspace documentation.

colours <- rainbow_hcl(4, start = 30, end = 300)
p %+% df2 + scale_fill_manual (values=colours) + opts(title="dynamic")

colours-dynamic

colours <- rainbow_hcl(4, start = 60, end = 240)
p %+% df2 + scale_fill_manual (values=colours) + opts(title="harmonic")

colours <- rainbow_hcl(4, start = 270, end = 150)
p %+% df2 + scale_fill_manual (values=colours) + opts(title="cold")

colours <- rainbow_hcl(4, start = 90, end = -30)
p %+% df2 + scale_fill_manual (values=colours) + opts(title="warm")

Colorspace using scale_fill_hue

It is also possible to use the colorspace palettes directly through scale_fill_hue(). However, ggplot2 version 0.8.2 produces somewhat different colours. Hadley explains: “scale_fill_hue generates n + 1 colours between start and end and then drops the last colour. That’s the best method I could come up with for when you have n colours spanning 0 to 360, but it’s not quite right when you restrict the range of the hues”. In the upcoming version this behaviour has been changed and I will be updating this post once the version update becomes available.

Version 0.8.3 improved the scale_fill_hue() function and the plots below are if not identical then at least very similar to the ones created with the help of colorspace package.
p %+% df2 + scale_fill_hue(c = 50, l = 70, h=c(30, 300)) + opts(title="scale_fill_hue-dynamic")

p %+% df2 + scale_fill_hue(c = 50, l = 70, h=c(60, 240)) + opts(title="scale_fill_hue-harmonic")

p %+% df2 + scale_fill_hue(c = 50, l = 70, h=c(270, 150)) + opts(title="scale_fill_hue-cold")

p %+% df2 + scale_fill_hue(c = 50, l = 70, h=c(90, -30)) + opts(title="scale_fill_hue-warm")

Custom Palette

If you need to use a specific colour palette (e.g. colours specified in your corporate brand manual), then this can be accomplished using scale_fill_manual(). There are several websites (ColorBlender, Kuler, COLOURlovers, to name just a few) which allow you to create your own palettes, or to use palettes created by other users. In the example below, Primary Peter 5 palette from ColorBlender.com is being used.

colours <- c("#2121D9", "#9999FF", "#D92121", "#21D921", "#FFFF4D", "#FF9326")
p + scale_fill_manual (values=colours) + opts(title="Primary Peter 5")

colours-primary_peter_5

6 Responses leave one →
  1. 2009 April 18

    (Apologies, this is the only way I could find to contact you).
    I notice that you provide a regular blog about R. I have recently started a wiki: http://riki.wikidot.com/
    It is intended to help the community with the understanding of R. The site is in its early stages and requires (a lot) more content. If you are able to lend a hand then that would be greatly appreciated. Maybe post some of you blog posts as guides…?
    Thanks.
    DM

    • 2009 April 19
      learnr permalink

      DM: I have nothing against including the blog posts in the wiki.

  2. 2009 October 20
    Thias permalink

    Hi,
    Thanks for all your very helpfull work.
    I’m just crasy because I don’t find the HEX code of the default palette used by ggplot2 ?
    Maybe there’s a simple way to show it ?
    Thanks

    • 2009 October 20
      learnr permalink

      If I am not mistaken then the continuous palette is generated with:
      colorRamp(c("#3B4FB8", "#B71B1A"), space="rgb", interpolate="linear")

  3. 2009 December 8

    Wonderful post,
    Thank you.

Trackbacks & Pingbacks

  1. links for 2009-05-05 | dekay.org

Leave a Reply

Note: You can use basic XHTML in your comments. Your email address will never be published.

Subscribe to this comment feed via RSS