[Rd] Small change to plot.xy

Peter Dalgaard BSA p.dalgaard@biostat.ku.dk
22 Nov 2002 14:01:10 +0100


Jonathan Rougier <J.C.Rougier@durham.ac.uk> writes:

> Hi everyone,
> 
> Is there any reason why we should not automatically coerce a factor
> supplied as an argument to col in a plotting function?  The following
> modification (to R-1.6.1) seems pretty harmless
> 
> > plot.xy
> function (xy, type, pch = 1, lty = "solid", col = par("fg"), 
>     bg = NA, cex = 1, ...) 
> {
>     if (is.factor(col)) 
>         col <- codes(col)
>     .Internal(plot.xy(xy, type, pch, lty, col, bg, cex, ...))
> }
> <environment: namespace:base>
> 
> and I think it is natural and not really wrong to want to type, say,
> 
> > data(iris)
> > pairs(iris[, 1:4], col = iris[, 5])
> 
> and get the colours.

It's not clear to me that you want codes() there. Consider

f <-factor(c("red","blue","green")) 
plot(1:3,col=codes(f))

which get coloured green, black, and red. Arguably better than to drop
codes() and get black, black, and black, but not by much. Alternatives
could be as.numeric() or as.character(), but it all gets a bit
arbitrary. I think I prefer the explicit style in any case

clr <- sort(c("red","blue","green"))
plot(1:3,col=clr[f])

(beware the ordering of levels in f)

-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard@biostat.ku.dk)             FAX: (+45) 35327907