Skip to content

again troubles with lattice

4 messages · Vladimir Eremeev, Brian Ripley, Deepayan Sarkar +1 more

#
Dear r-help community,

  Thank you for your previous answers!
  
  Now I have strange behaviour of the lattice library funcitons.
  They do not draw graphics in the file when called from the script.
  I created the script file, called, for example, "a.R", containing
  the following
  
library(lattice);
trellis.device(device="png",
               filename="a.png",
               color = FALSE,
               bg = "white",
               width=600,
               height=800
              );

xyplot(ac15$value~ac15$year|factor(lon),data=ac15,
       type="o",
       layout=c(1,18),
       xlab="year",
       );
dev.off();


The structure of ac15 data frame is like following
year    value    lon
1979  93.428747  0
1979  87.298608  20
1979  78.506340  40
...
1979  45.567890  340
1980  60.815289  0
1980  49.630904  20
1980  24.981362  40
...

I execute command source("a.R") it works and after its running I've
got file a.png in the working directory. But it contains only white
background field and no graphs.

If I try copy commands of the script in clipboard and paste them to
the R console the graphs appear in the file.
#
On Thu, 8 May 2003, Wladimir Eremeev wrote:

            
They do when called from a script running directly.  It's calling the 
script file from source() that gives the problem.

You haven't actually asked for anything to be plotted!  xyplot returns a
plottable object, which when *printed* is plotted.  Auto-printing works at
the top level in R, but not inside source.  You need to wrap your lattice
calls in print().

[...]
And that is `running the script'.
#
On Thursday 08 May 2003 03:54 am, Wladimir Eremeev wrote:
You have to print the result of xyplot, which does not happen inside another 
function unless you explicitly call print().

This is obviously somewhat confusing because base R graphics does not behave 
this way, and has led to many similar questions on this list. help(xyplot) 
has:

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.


and help(Lattice) 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.


Can you suggest any other place in the documentation where explaining this 
would have helped you find it ?

Deepayan
#
On Thu, 8 May 2003, Deepayan Sarkar wrote:

            
Perhaps a FAQ entry?

Q: Why don't lattice/trellis graphics work?

A: The most likely reason is that you forgot to tell R to display the
graph.  Lattice functions such as xyplot() create a graph object, but do
not display it (the same is true of Trellis graphics in S-PLUS).  The
print() method for the graph object produces the actual display.  When you
use these functions interactively at the command line the result is
automatically print()ed, but in source() or inside your own functions you
will need an explicit print() statement.


	-thomas