[R] Scatterplot - symbols coded by factor

Prof Brian Ripley ripley at stats.ox.ac.uk
Sat Feb 10 16:42:53 CET 2001

On Sat, 10 Feb 2001, Emmanuel Paradis wrote:

> At 08:51 10/02/01 +0000, Brian D Ripley wrote:
> >On Fri, 9 Feb 2001, Matt Pocernich wrote:
> >
> >> Hi - I would like to create a scatterplot with the symbols coded by a
> factor in each record.  Is their a better way than what I have used below?
> I attempted to use the plot.factor, but cannot force it to make a
> scatterplot instead of boxplots.  I have considerd making a function with a
> 'for' statement for each level of the factor.  Is there a better way for
> when I am dealing with more levels of factors?
> >>
> >> Thanks,  Matt Pocernich
> >>
> >> Y <- c(40,38,30,47,25,32,13,35,27,24,20,13)
> >> X<-c(110,75,93,97,60,75,38,140,62,90,45,59)
> >> manuf <- factor(rep(LETTERS[1:3],c(4,4,4)))
> >> data3 <- data.frame(X,Y,manuf)
> >> attach(data3)
> >>
> >> plot(X, Y, type="n")
> >> points(X[manuf=="A"], Y[manuf=="A"], pch = "A")
> >> points(X[manuf=="B"], Y[manuf=="B"], pch = "B")
> >> points(X[manuf=="C"], Y[manuf=="B"], pch = "C")
> >
> >You can use
> >
> >> plot(X, Y, type="n")
> >> points(X, Y, pch=as.character(manuf))
> The following (inspired from demo(graphics)) may also be useful:
> 	plot(X, Y, pch=levels(manuf)[codes(manuf)])

That will only work if the levels are alphabetical order. Best avoid
codes() unless you really need it. You could use
levels(manuf)[unclass(manuf)], but as.character(manif) is simpler (if
marginally slower).

See the warning on the help page:

     For an unordered factor, an alphabetical ordering of the levels is
     assumed, i.e the level that is coded 1 is the one whose name is
     sorted first according to the prevailing collating sequence.
     Warning: the sort order may well depend on the locale, and should
     not be assumed to be ASCII.

The reason for the warning is that in the C locale it is boBO, in the
en_UK locale on SunOS (and analogously on Windows)  bBoO, and one might
expect BbOo as in a book index.

> The example in demo(graphics) shows a case where the user wants different
> colours for the symbols with respect to a factor (here the Iris species):
> 	pairs(iris[1:4], main = "Edgar Anderson's Iris Data",
> 		pch = 21, bg = c("red", "green3", "blue")[codes(iris$Species)])

Yes, it does. Fortunately the levels are in alphabetical order.  I suspect
that example is so old that the meaning of codes() has changed since that
was written.

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 272860 (secr)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch

More information about the R-help mailing list