[R] Lattice plot within a "for" loop does not happen?

Barry Rowlingson B.Rowlingson at lancaster.ac.uk
Fri May 13 16:24:04 CEST 2005


> 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




More information about the R-help mailing list