Colour wheels in R

Regular readers will know I use the R package to produce most of the charts that appear here on the blog. Being more quantitative than artistic, I find choosing colours for the charts to be one of the trickiest tasks when designing a chart, particularly as R has so many colours to choose from.

In R, colours are specified by name, with names ranging from the obvious “red”, “green” and “blue” to the obscure “mintycream”, “moccasin” and “goldenrod”. The full list of 657 named colours can be found using the colors() function, but that is a long list to wade through if you just want to get exactly the right shade of green, so I have come up with a shortcut which I thought I would share here*.

Below is a simple function called col.wheel which will display a colour wheel of all the colours matching a specified keyword.

col.wheel <- function(str, cex=0.75) {
	cols <- colors()[grep(str, colors())]
	pie(rep(1, length(cols)), labels=cols, col=cols, cex=cex)
	cols
	}

To use the function, simply pass it a string:

col.wheel("rod")

As well as displaying the colour wheel below, this will return a list of all of the colour names which include the specified string.

 [1] "darkgoldenrod"        "darkgoldenrod1"       "darkgoldenrod2"
 [4] "darkgoldenrod3"       "darkgoldenrod4"       "goldenrod"
 [7] "goldenrod1"           "goldenrod2"           "goldenrod3"
[10] "goldenrod4"           "lightgoldenrod"       "lightgoldenrod1"
[13] "lightgoldenrod2"      "lightgoldenrod3"      "lightgoldenrod4"
[16] "lightgoldenrodyellow" "palegoldenrod"

"Rod" colour wheel
In fact, col.wheel will accept a regular expression, so you could get fancy and ask for colours matching “r.d” which will give you all the reds and the goldenrods.

This trick does have its limitations: you are not likely to find “thistle”, “orchid” or “cornsilk” this way, but I have found it quite handy and others may too.

*My tip was inspired by this page about R colours.

Possibly Related Posts (automatically generated):

11 thoughts on “Colour wheels in R

  1. Drew

    I thought this was really interesting, so I added to your function and made it accept the partial name of a color and then output the same chart/list for nearby colors (by hue, which I figured out how to do from the link you provided). Here it is:

    col.nearby <- function(str, cex=0.75) {
    vcol <- colors()
    RGBColors <- col2rgb(colors()[1:length(colors())])
    HSVColors <- rgb2hsv( RGBColors[1,], RGBColors[2,], RGBColors[3,],
    maxColorValue=255)
    SortColors <- order( HSVColors[1,], HSVColors[2,], HSVColors[3,] )
    vcol<-vcol[SortColors]
    ind <- grep(str, vcol)
    cols <- vcol[ind]
    for(i in ind){
    cols <- append(vcol[i+1],cols)
    cols <- append(vcol[i-1],cols)
    cols <- append(vcol[i+2],cols)
    cols <- append(vcol[i-2],cols)
    cols <- append(vcol[i+3],cols)
    cols <- append(vcol[i-3],cols)
    }
    cols <- sort(cols[!duplicated(cols)])
    pie(rep(1, length(cols)), labels=cols, col=cols, cex=cex)
    cols
    }

    It's just quickly hacked together, so it's probably heinously inefficient. And if you give it something very ambiguous, it blows up a bit. But if you know one color that you like, but want to find something similar, it's still fairly useful, I think.

  2. Stubborn Mule Post author

    @Drew: thanks again. I have now posted a revised version based on your idea. The main changes are (a) a parameter to specify how many colours to include on either side of the text matches and (b) I have kept the results sorted in hue order, while your version sorts the final results by name.

  3. Pingback: More colour wheels

  4. NJBA

    “Lisa: I’d like 25 copies on Goldenrod.
    Clerk: Right.
    Lisa: 25 on Canary.
    Clerk: Mmhmm.
    Lisa: 25 on Saffron.
    Clerk: All right.
    Lisa: And 25 on Paella.
    Clerk: Ok, 100 yellow.”

  5. Tel

    You know that you are not forced to stick to the named colours? You can just generate your own colours as RGB values.

    showcol <- function( cols ) { pie( rep(1, length( cols )), col=cols, labels=cols ); }

    showcol( hcl( h=seq(0, 360, length=20 ), c=80, l=50 ))

    showcol( heat.colors( 20 ))

    showcol( terrain.colors( 20 ))

    showcol( rainbow( 20 ))

  6. Stubborn Mule Post author

    Thanks Tel…RGB is handy too. For me the appeal of the named colours is to constrain myself to a relatively small, manageable set of colours: I am already rather challenged in the visual-design department! Functions like heat.colors and rainbow are useful in that regard too.

  7. Pingback: Joy of colour | socialdatablog

Leave a Reply