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

From: Prof Brian Ripley <ripley_at_stats.ox.ac.uk>
Date: Fri 18 Nov 2005 - 17:08:35 GMT

On Fri, 18 Nov 2005, Christoph Buser wrote:

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?
>

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@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

______________________________________________
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
```
Received on Sat Nov 19 04:23:59 2005

This archive was generated by hypermail 2.1.8 : Mon 20 Feb 2006 - 03:21:33 GMT