**Stubborn Mule » R**, and kindly contributed to R-bloggers)

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"

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.

**leave a comment**for the author, please follow the link and comment on his blog:

**Stubborn Mule » R**.

R-bloggers.com offers

**daily e-mail updates**about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...