[R] Multiple colors in plots/lookup function

John Fox jfox at mcmaster.ca
Thu Oct 17 17:34:05 CEST 2002

Dear Martin,

At 03:21 PM 10/17/2002 +0200, Martin Maechler wrote:
> >>>>> "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.

Actually, scatterplot.matrix calls pairs; it just makes it simpler (in my 
opinion) to get a graph with features like points plotted with different 
colours and symbols, a legend, univariate displays down the main diagonal, 
concentration ellipses, regression lines, etc.


John Fox
Department of Sociology
McMaster University
Hamilton, Ontario, Canada L8S 4M4
email: jfox at mcmaster.ca
phone: 905-525-9140x23604
web: www.socsci.mcmaster.ca/jfox

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