Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
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.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.