Last Saturday, I posted a question about how to get a pairs( )
plot to plot data subsets with different colors and/or symbols.
I have copied the respondents messages below. Many thanks
to Dr. Madeline Bauer, Dr. Andy Liaw, Dr. Bert Gunter, and Dr.
Nick Ellis for their helpful replies.
With gratitude,
david paul
--------------------------------------------------
ORIGINAL MESSAGE:
I have a factor variable f1 which I would like to
use to get pairs( ) to plot subsets of the specified
data.frame in various colors/symbols.
I am aware that panel = function(x,y) { } can be used
to modify the pairs( ) plots... my naïve way to try to
take advantage of this was
pairs
(
data = foo.frame,
panel=function(x,y)
{
if( x != foo.frame$f1 & y != foo.frame$f1 )
{
if( foo.frame$f1 == 1 )
{
col = 1;
points(x,y);
}
else
{
col = 2;
points(x,y);
}
}
}
)
This failed to work in spectacular fashion. Obviously, I
want to exclude f1 from the points(x,y) calls since I don't
care to create panels for f1.
--------------------------------------------------
REPLY #1
Hi David,
Assuming I understand what you are looking to do, I think
you may find the trellis function named splom helpful. Also
look at the example for figure 3.14 (p. 82) in V&R 3rd edition
(you DO have V&R?).
Good luck,
Madeline
--------------------------------------------------
REPLY #2
[If you don't want f1 in the pairs plot, you need to exclude it
from the first argument to pairs().]
This is one place where R is clearly better than Splus. You can do
something like
pairs(mat, col=(1:length(levels(myfactor)))[as.numeric(myfactor)])
and the points will be colored by the levels of myfactor.
To do something like it in Splus, you can try:
pairs(mat, panel=function(x, y, myfactor, ...) {
nlvl <- length(levels(myfactor))
for (i in 1:nlvl) points(x, y, col=i, ...)
}, myfactor)
[Untested! I don't have access to Splus at this minute.]
HTH,
Andy Liaw
--------------------------------------------------
REPLY #3
David:
Whoops! if() requires a logical not a vector of logicals, so
that produces an error.
I have found that the following works in R, but I have not
tried it in S-Plus -- because of different scoping conventions,
it may not.
pairs(data=foo.frame,panel=panel.groups,f=foo.frame$f1)
panel.groups=function(x,y,f)
{
for(i in levels(f)){
choose<-f==i
points(x[choose],y[choose],col=as.numeric(i),pch=as.numeric(i))
}
This produces a warning message in R, but it works.Hope it works
for you.
Cheers,
Bert Gunter
Biometrics Research RY 33-300
Merck & Company
--------------------------------------------------
REPLY #4
David:
You may need to modify the [earlier] code that I sent you to:
for( i in as.character(levels(f)){ ...
or maybe
.... col=as.numeric(as.character(i)), ...
Depends on what methods there are and exactly how they're defined.
That is, of course, if you don't encounter the scoping problems.
-- Bert
pairs(data=foo.frame,panel=panel.groups,f=foo.frame$f1)
panel.groups=function(x,y,f)
{
for(i in levels(f)){
choose<-f==i
points(x[choose],y[choose],col=as.numeric(i),pch=as.numeric(i))
}
This produces a warning message in R, but it works.Hope it
works for you.
Cheers,
Bert Gunter
Biometrics Research RY 33-300
Merck & Company
--------------------------------------------------
REPLY #5
Try this
df <- data.frame(x=rep(1:2,10)+rnorm(20)/5,
y=rep(1:2,10)+rnorm(20)/5,z=rep(2:1,10)+rnorm(20)/5,
f1=factor(rep(1:2,10)))
pairs(df[,1:3],panel=function(x,y,...,f1) {
s<- f1==levels(f1)[1]
points(x[s],y[s],col=2,...)
points(x[!s],y[!s],col=3,...)
},f1=df$f1)
Nick Ellis
CSIRO Marine Research mailto:Nick.Ellis@csiro.au
PO Box 120 ph +61 (07) 3826 7260
Cleveland QLD 4163 fax +61 (07) 3826 7222
Australia http://www.marine.csiro.au
|