[Rd] Small change to plot.xy

Duncan Murdoch murdoch@stats.uwo.ca
Fri, 22 Nov 2002 09:55:14 -0500


On 22 Nov 2002 14:01:10 +0100, Peter Dalgaard BSA
<p.dalgaard@biostat.ku.dk> wrote in message
<x2lm3lvjvt.fsf@biostat.ku.dk>:


>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 you want it to act as though it is coercing everything to
character.  If something prints as "1", then it should act as though
it is colour 1.  If it prints as "red", it should be red.

I don't think you actually want to coerce everything to character;
that's too inefficient in the case of numeric colours, but it should
act as if you did.

I think this version gives that behaviour, but I might have the wrong
test...

plot.xy <- function (xy, type, pch = 1, lty = "solid", col =
par("fg"), 
    bg = NA, cex = 1, ...) 
{
    if (!is.numeric(col)) 
        col <- as.character(col)
    .Internal(plot.xy(xy, type, pch, lty, col, bg, cex, ...))
}

This doesn't help with Jonathan's iris example; in that case, I think
the fix should be to abort with the declaration that "setosa" etc.
aren't colours.  Alternatively, they could be coerced to numeric
colours (preferably not all black), but at the very least a warning
should be given when that is done.

Duncan Murdoch