Skip to content

ggplot2: Qualitative Colour Palettes

April 15, 2009

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.

df <- data.frame(x=letters[1:8])p <- qplot(data=df, x, 1, geom="bar", stat="identity", fill=x) +
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")



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")


brewerplot ("Set2")

brewerplot ("Set3")

brewerplot ("Accent")

brewerplot ("Dark2")

brewerplot ("Paired")

brewerplot ("Pastel1")

brewerplot ("Pastel2")


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 <- 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 is being used.

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


12 Comments leave one →
  1. April 18, 2009 2:57 pm

    (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:
    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…?

    • learnr permalink
      April 19, 2009 11:12 pm

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

  2. Thias permalink
    October 20, 2009 4:39 pm

    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 ?

    • learnr permalink*
      October 20, 2009 6:51 pm

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

  3. December 8, 2009 11:48 pm

    Wonderful post,
    Thank you.

  4. baptiste permalink
    December 22, 2009 11:00 pm


    Another option is in the colour schemes from the dichromat package,


    dichroplot <- function (palette) {
    p %+% data.frame(x=letters[1:5]) +
    scale_fill_dichromat(palette = palette) + opts(title=palette)
    }, llply(ggExtra::dichromat.names, dichroplot))

  5. Neetash M R permalink
    August 24, 2016 3:42 pm

    Really nice consolidation! Thank you!
    How can I get the list of color codes for each colour in a set?

    • learnr permalink*
      November 14, 2016 3:49 am

      Try ggplot2_build(d) where d is ggplot2 plot.

  6. Matthew Haas permalink
    October 28, 2016 6:26 pm

    This is a long shot, but I’m looking for specific color names. I’m trying to recreate a plot that a package gave me using ggplot2 with a custom script so that I can tweak the image a little bit. Do you know where I might be able to find the names of the colors used by scale_fill_hue()? I’ve found a comprehensive collection of R color names, but it’s a little hard to match exactly using this method. (I’m trying to plot inside data.table)

    • learnr permalink*
      November 14, 2016 3:48 am

      The examples here are not working in current version of ggplot2, nevertheless, try ggplot_build(d) where d is your ggplot2 object. This should give you what you are after.


  1. links for 2009-05-05 |
  2. GGplot2 – Ekaterina Volkova

Leave a Reply

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

You are commenting using your 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

%d bloggers like this: