George W. Gilchrist
2005-May-13 14:09 UTC
[R] Lattice plot within a "for" loop does not happen?
I am trying to do a series of xyplots plots, using a "for" loop to substitute the appropriate variables for each plot. The basic command works fine by itself and produces a nice plot: > i<-3 > trellis.device(theme="col.whitebg") > xyplot(as.formula(paste(tmp00[2*i], "~ ", tmp00[(2*i)-1], + "|Blastomere+Phenotype", sep="")), + data=tmp1, + panel=function(x,y,...){ + panel.xyplot(jitter(x), jitter(y), pch=16, col="red") + if (max(x, na.rm=T)!=0.0) panel.xyplot(x, y, type="r", lwd=2) + }) > BUT, when I stick this in a loop, I get a bunch of blank graphics devices. This happens even if the loop only executes once. I could just go through and do these one by one, but I was curious if I was overlooking something obvious. Thank you for any advice. =================================================================George W. Gilchrist Email #1: gwgilc at wm.edu Department of Biology, Box 8795 Email #2: kitesci at cox.net College of William & Mary Phone: (757) 221-7751 Williamsburg, VA 23187-8795 Fax: (757) 221-6483 http://gwgilc.people.wm.edu/
Hi many times answered. Just enter lattice loop R into Google and you are there Explicite printing lattice object is what you need. Cheers Petr On 13 May 2005 at 10:09, George W. Gilchrist wrote:> I am trying to do a series of xyplots plots, using a "for" loop to > substitute the appropriate variables for each plot. The basic command > works fine by itself and produces a nice plot: > > > i<-3 > > trellis.device(theme="col.whitebg") > > xyplot(as.formula(paste(tmp00[2*i], "~ ", tmp00[(2*i)-1], > + "|Blastomere+Phenotype", sep="")), > + data=tmp1, > + panel=function(x,y,...){ > + panel.xyplot(jitter(x), jitter(y), pch=16, col="red") if > + (max(x, na.rm=T)!=0.0) panel.xyplot(x, y, type="r", > lwd=2) > + }) > > > > BUT, when I stick this in a loop, I get a bunch of blank graphics > devices. This happens even if the loop only executes once. I could > just go through and do these one by one, but I was curious if I was > overlooking something obvious. Thank you for any advice. > > =================================================================> George W. Gilchrist Email #1: gwgilc at wm.edu > Department of Biology, Box 8795 Email #2: kitesci at cox.net > College of William & Mary Phone: (757) 221-7751 > Williamsburg, VA 23187-8795 Fax: (757) 221-6483 > http://gwgilc.people.wm.edu/ > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.htmlPetr Pikal petr.pikal at precheza.cz
Barry Rowlingson
2005-May-13 14:24 UTC
[R] Lattice plot within a "for" loop does not happen?
> BUT, when I stick this in a loop, I get a bunch of blank graphics > devices. This happens even if the loop only executes once. I could just > go through and do these one by one, but I was curious if I was > overlooking something obvious. Thank you for any advice.You're overlooking something like line 800 of the documentation for xyplot: Value: An object of class ``trellis''. The `update' method can be used to update components of the object and the `print' method (usually called by default) will plot it on an appropriate plotting device. xyplot doesn't actually make any marks on the screen. Oh no. It returns an object. You have to make that object make the marks on the screen. This happens automatically when you run something interactively, but not inside a function. So wrap your xyplot call in a print() function inside your loop: for(i in 1:10){ print(xyplot(....whatever....)) } Its probably in the R-FAQ as well, since my original feeling was that this behaviour was chosen in order to confuse people and see how many people read the FAQ... :) Baz