[R] xyplot and for loops

Dave Atkins datkins at u.washington.edu
Wed May 5 00:40:34 CEST 2004


I'm attempting to use xyplot() within a for() loop to plot the relationship 
between a DV and a series of predictor variables, split by 2 conditioning 
variables.  However, xyplot() does not "seem" to be recognized within the for() 
loop; I don't receive any error message, but nothing is plotted and a plotting 
device is not opened.  When I use the generic function plot(), everything works 
as expected.  I'm using R v1.9.0 and lattice v0.9-11.

Here's a trivial example:

### create single outcome and two predictors
 > y.tmp <- rnorm(20) ; x1.tmp <- rnorm(20) ; x2.tmp <- rnorm(20)
### combine into data.frame
 > tmp.df <- data.frame(y.tmp, x1.tmp, x2.tmp)
 > tmp.df
         y.tmp       x1.tmp      x2.tmp
1   1.5022759 -0.150326662 -1.36627981
[snip]
20  1.2667825 -0.070356651 -0.38433160

### simple loop calling the columns of tmp.df to specify x variables
 > for (i in 2:3){
+ plot(tmp.df$y.tmp ~ tmp.df[,i])
+ }
### works fine for plot()

### try the same with xyplot()
 > for (i in 2:3){
+ xyplot(tmp.df$y.tmp ~ tmp.df[,i])
+ }
### nothing happens; no error msg; no plotting device opened

 > xyplot(tmp.df$y.tmp ~ tmp.df[,2])
### without loop works fine and plots relationship of x1.tmp and y.tmp

Can someone see what I'm missing?  Any directions/pointers appreciated.

cheers, Dave
datkins at u.washington.edu




More information about the R-help mailing list