[Rd] using a factor as col argument in plot:

Prof Brian Ripley ripley at stats.ox.ac.uk
Fri Nov 18 18:08:35 CET 2005


On Fri, 18 Nov 2005, Christoph Buser wrote:

> Dear R core team

and R-devel-listers.

> Using the following code produces an empty plot (similar
> to col = NA):
>
>> plot(1:9, col = factor(rep(1:3,3), labels = c("red", "blue", "black")))
>
>
> My question: Shouldn't one get at least a warning (or an error)
> 	     if one tries to use a factor as col argument?
>
> Thanks for an answer.

I can read that two ways

1) No, it should not give a warning, as it is programmed to take all
invalid values as 0.

2) Is the way it is programmed sensible (yes) or desirable (no)?

The actual code is

/* Convert a sexp element to an R  color desc */
/* We Assume that Checks Have Been Done */

unsigned int RGBpar(SEXP x, int i)
{
     int indx;
     if(isString(x)) {
 	return str2col(CHAR(STRING_ELT(x, i)));
     }
     else if(isInteger(x) || isLogical(x)) {
 	if(INTEGER(x)[i] == NA_INTEGER)
 	    /*
 	     * Paul 01/07/04
 	     * Used to be set to NA_INTEGER (see comment in name2col).
 	     */
 	    return R_TRANWHITE;
 	indx = INTEGER(x)[i] - 1;
 	if(indx < 0) return Rf_dpptr(CurrentDevice())->bg;
 	else return R_ColorTable[indx % R_ColorTableSize];
     }
     else if(isReal(x)) {
 	if(!R_FINITE(REAL(x)[i]))
 	    /*
 	     * Paul 01/07/04
 	     * Used to be set to NA_INTEGER (see comment in name2col).
 	     */
 	    return R_TRANWHITE;
 	indx = REAL(x)[i] - 1;
 	if(indx < 0) return Rf_dpptr(CurrentDevice())->bg;
 	else return R_ColorTable[indx % R_ColorTableSize];
     }
     return 0;		/* should not occur */
}

but I could see no checks of type in any of the calling functions. Adding 
a warning would be a good idea.


-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



More information about the R-devel mailing list