In response to my post about colour wheels, I received a suggested enhancement from Drew. The idea is to first match colours based on the text provided and then add nearby colours. This can be done by ordering colours in terms of hue, saturation, and value. The result is a significant improvement and it will capture all of those colours with more obscure names.
Here is my variant of Drew’s function:
col.wheel <- function(str, nearby=3, cex=0.75) {
cols <- colors()
hsvs <- rgb2hsv(col2rgb(cols))
srt <- order(hsvs[1,], hsvs[2,], hsvs[3,])
cols <- cols[srt]
ind <- grep(str, cols)
if (length(ind) <1) stop("no colour matches found",
call.=FALSE)
s.ind <- ind
if (nearby>1) for (i in 1:nearby) {
s.ind <- c(s.ind, ind+i, ind-i)
}
ind <- sort(unique(s.ind))
ind <- ind[ind <= length(cols)]
cols <- cols[ind]
pie(rep(1, length(cols)), labels=cols, col=cols, cex=cex)
cols
}
I have included an additional parameter, nearby, which specifies the range of additional colours to include. A setting of 1 will include colours matching the specified string and also one colour on either side of each of these. By default, nearby is set to 3.
The wheel below shows the results for col.wheel(“thistle”, nearby=5). As well as the various shades of “thistle”, this also uncovers “plum” and “orchid”.
This is far more powerful than the original function: thanks Drew.
Possibly Related Posts (automatically generated):
- Colour wheels in R (5 November 2011)
- Generate your own Risk Characterization Theatre (25 October 2010)
- Getting Protovis working on WordPress (6 September 2010)
- Junk Charts #4 – Puns are dangerous (31 August 2010)
Wow, I already loved this function in its previous guise, now I love it even more. Thanks to both!
@Chris: glad to be of help!