[R] Multiple colors in plots/lookup function

Martin Maechler maechler at stat.math.ethz.ch
Thu Oct 17 15:21:13 CEST 2002

>>>>> "John" == John Fox <jfox at mcmaster.ca>
>>>>>     on Thu, 17 Oct 2002 07:28:42 -0400 writes:

    John> Dear David, At 12:04 AM 10/17/2002 -0400, David
    John> Forrest wrote:

    >> I'd like to do something like:
    >> n<-100 zz<-cbind(rnorm(n),rnorm(n),floor(runif(n)*3+1))
    >> colors<-c("red","green","blue")
    >> plot(zz,col=colors(zz[3]))
    >> and have a matrix of scatterplots colored by class.  The
    >> above does not work, of course, but I'm not sure exactly
    >> what function I'm looking for.

    John> A couple of people have already mentioned that
    John> specifying col=colors[zz[,3]] will colour the points
    John> in a scatterplot (not a scatterplot matrix) by the
    John> values of zz[,3]. 

yes, even in a scatterplot matrix.
Try a very slight variation of the above :

  n <- 100
  zz <- data.frame(Z1 = rnorm(n), Z2 = rnorm(n), Ufact = floor(runif(n)*3+1))
  colors <- c("red","green","blue")
  plot(zz, col = colors[zz[, 3]])

i.e., the only practical difference is that I used 
data.frame() instead of

--> which changes things:  The plot() method for a data.frame calls
pairs() which *is* the same as a scatterplot matrix.

    John> 	Since there are just two other
    John> columns (the third column of zz is ignored in the
    John> first argument to plot), you get a scatterplot, not a
    John> scatterplot matrix, and it's hard to see how a
    John> scatterplot matrix would be relevant.


    John> If, however, you want a scatterplot matrix of pairwise
    John> plots for three or more variables coloured by the
    John> levels of a factor, the scatterplot.matrix function in
    John> the car package should do what you want.

I don't see why pairs()  {or as mentioned,  plot( ) for a data.frame}
shouldn't be sufficient.

Martin Maechler <maechler at stat.math.ethz.ch>	http://stat.ethz.ch/~maechler/
Seminar fuer Statistik, ETH-Zentrum  LEO C16	Leonhardstr. 27
ETH (Federal Inst. Technology)	8092 Zurich	SWITZERLAND
phone: x-41-1-632-3408		fax: ...-1228			<><
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