[Rd] col2rgb, factors & documentation

Bryan Hanson hanson at depauw.edu
Fri Jun 7 19:59:52 CEST 2013


Hello Core Folk.

Quite by accident I discovered today that col2rgb, when fed factors, acts on the integer representation of those factors:

td <- as.factor(rainbow(5)) # not how I discovered the issue!
td
res <- col2rgb(td)
res
# but
col2rgb("#FF0000FF") # this is td[1] but does not produce the same answer
res[,1]
# moreover
as.integer(td)
col2rgb(5) # the integer value of the first level
# and this is res[,1] which by the way is palette()[5], aka "cyan"

?col2rgb says:
col	
vector of any of the three kinds of R color specifications, i.e., either a color name (as listed by colors()), a hexadecimal string of the form "#rrggbb" or "#rrggbbaa" (see rgb), or a positive integer i meaning palette()[i]. Non-numeric values are coerced to character.

So in the example the factor is being taken as its integer representation, and finding an integer as the input value, col2rgb goes to palette().

Perhaps this is a nuance that belongs in the documentation?

Thanks, Bryan
***********
Bryan Hanson
Professor of Chemistry & Biochemistry
DePauw University



More information about the R-devel mailing list