Skip to content

Lattice plot within a "for" loop does not happen?

5 messages · George W. Gilchrist, PIKAL Petr, Barry Rowlingson +2 more

#
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:

            
Petr Pikal
petr.pikal at precheza.cz
#
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
#
Barry Rowlingson wrote:

            
Baz, actually, printing happens only under two circumstances, AFAIK:
1. by wrapping in print()
2. automatically by evaluating an expression (which might be the object 
name only) in the top level (R_GlobalEnv), but only if no assignment 
takes place.

In particular, automatical (point 2 above) printing happens also in 
non-interactive sessions like
   R CMD BATCH
calls.

The idea of returning an object is very nice, I think. You can calculate 
on the object and print the modified object (well, in fact, I rarely use 
lattice myself, though).

Best,
Uwe
#
On Friday 13 May 2005 09:24 am, Barry Rowlingson wrote:
As well as the much much shorter help(Lattice), which has: 

Note:

     High level Lattice functions (like 'xyplot') are different from
     conventional S graphics functions because they don't actually draw
     anything. Instead, they return an object of class ``trellis''
     which has to be then 'print'ed. This often causes confusion when
     the high level functions are called inside another function (most
     often 'source') and hence don't produce any output.

This page is pointed to from every conceivable place, including the 
Description, which says:

Description:   Implementation of Trellis Graphics. See ?Lattice for a
               brief introduction
No comments on that :)

However, let's say I want to use pseudo-random numbers to study the behaviour 
of sample correlation in uncorrelated observations. To this end, I do:
[1] 0.3899596

I do it a few more times:
[1] 0.6481215
[1] -0.02100718
[1] -0.01141006

but then I get tired and try:
+   cor(rnorm(10), rnorm(10))
+ }
resulting in nothing!!! 

Strange how no one ever (or at least any more) complains about this behavour, 
which should be exactly as ``confusing''. 

The upshot, of course, can be summarized by a now famous observation made 
slightly more than a decade ago:

http://groups-beta.google.com/group/comp.sys.next.advocacy/msg/92532d5651e795dc

Deepayan