[Rd] Small change to plot.xy

Thomas Lumley tlumley@u.washington.edu
Sat, 23 Nov 2002 10:43:48 -0800 (PST)


On Sat, 23 Nov 2002, Jonathan Rougier wrote:

> OK -- so there are two objections to the proposal:
>
> 1) The factor might be a colour specifier
>

The function below might help
It checks:
  - are the levels colour names
  - are they numbers
  - are they color specifiers like #a0f3d2
otherwise it returns the underlying codes with unclass().

On the issue of whether the current default palettes are ideal I can quote
Paul Murrell's talk at the JSM ("The only word for this is
`embarrassing'").  Improved palettes are planned, and one step in that
direction is the RColorBrewer package, which supplies color schemes
useful for images, maps and barplots.

	-thomas


factor2color<-function(color){
   nms<-as.character(color)
   n<-length(nms)
   ## are they color names
   is.color<-nms %in% colors()
   if (all(is.color)) return(nms)
   if (sum(is.color)>2 && mean(is.color)>2/3)
	warning("Not all factor names are colors")
   ## are they numbers
   m<-length(grep("[^:digit:]",nms))
   if (m==0 && all(!is.na(nums<-as.numeric(nms))))
 	return(nums)
   ## are they #a0bfe4
   n<-length(nms)
   m<-length(grep("#[0-9A-Fa-f]{6,6}",nms))
   if(m==n)
      return(nms)
   ## otherwise just return numbers
   return(unclass(color))

}