[Rd] Small change to plot.xy

Jonathan Rougier J.C.Rougier@durham.ac.uk
Sat, 23 Nov 2002 10:53:48 +0000

ripley@stats.ox.ac.uk wrote:
> On Sat, 23 Nov 2002, Jonathan Rougier wrote:
> > ripley@stats.ox.ac.uk wrote:
> >
> > > > I can see that both of these have merit, but I think they are both easy
> > > > to work around, if necessary.  No-one has disagreed that it's natural to
> > > > want to pass a factor to col, and I believe that the vast majority of
> > > > times when this occurs the factor is not designed explicitly to paint
> > > > the points.
> > >
> > > I did disagree.  I don't see why users should not explicitly map the
> > > factor levels to colours, which takes only a few extra characters.
> > > People have been doing this for a decade in S without objecting.
> > > Why is it worth complicating R for?
> >
> > That's the difference between us: you think they should have to type a
> > few extra characters to achieve a natural result, and I don't.  It's two
> > extra lines in the source and an extra line in the help file -- I don't
> > call this a complication and I think that the next generation of
> > statisticians will be that much more taken with R (as opposed to, say,
> > SPSS) if we take the trouble to make the default behaviour as intuitive
> > as possible.
> No, the difference is that what you find `intuitive' other people find
> perverse.  Giving a factor of colour names and getting a different set of
> colours is perverse.  If the intention is not clear, it is more
> `intuitive' to give an error than to guess incorrectly.
> Does SPSS actually do this, that is arbitrarily assign colours to
> categories that might be names of colours?

I think you know the answer to that!  That wasn't my point.  I give
statistical computing practicals using the same material to two similar
groups of students, some using SPSS and some using R.  Right now, the
SPSS students are getting a lot more experience of data analysis.  My
SPSS students put Species in the "colour points by" box.  You and I may
disagree on what is natural and what is peverse, but my R students want
to do "col = iris$Species" and I am sympathetic.  

If your objection is overwriting colours then let's go with Robert's
suggestion of checking for this explicitly, maybe something along the
lines of

if (is.factor(col)) {
  tmp <- as.character(col)
  if (all(tmp %in% colors()))
    col <- tmp
    col <- unclass(col)

which has the desired effect:

> data(iris)
> iris$color <- c("red", "lightgreen", "slateblue")[unclass(iris$Species)]
> pairs(iris[, 1:4], col = iris$Species) # black, red, green
> pairs(iris[, 1:4], col = iris$color)   # as above


Jonathan Rougier                       Science Laboratories
Department of Mathematical Sciences    South Road
University of Durham                   Durham DH1 3LE
tel: +44 (0)191 374 2361, fax: +44 (0)191 374 7388