Skip to content

Multiple colors in plots/lookup function

3 messages · Martin Maechler, John Fox

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

            
A couple of people have already mentioned that specifying 
col=colors[zz[,3]] will colour the points in a scatterplot (not a 
scatterplot matrix) by the values of zz[,3]. Since there are just two other 
columns (the third column of zz is ignored in the first argument to plot), 
you get a scatterplot, not a scatterplot matrix, and it's hard to see how a 
scatterplot matrix would be relevant.

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

Regards,
John


-----------------------------------------------------
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
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
cbind()

--> 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.

indeed!

    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.

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

Regards,
  John

-----------------------------------------------------
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._