[R] Properly initializing a plot

Martin Maechler maechler at stat.math.ethz.ch
Wed Sep 4 08:56:31 CEST 2002


>>>>> "Frank" == Frank E Harrell <fharrell at virginia.edu>
>>>>>     on Tue, 3 Sep 2002 12:30:01 -0400 writes:

    Frank> I have many functions for high-level plotting that
    Frank> early on in their code do something like
    Frank> strwidth('string', units='inches') before plotting.
    Frank> I do this before plot() to set the correct margins
    Frank> with par(mai=...), to leave room for wide text on the
    Frank> left or right margins.  If the plotting device is not
    Frank> opened strwidth( ) causes an error that plot.new()
    Frank> needs to be called.  

yes.  

  > x11()
  > strwidth("a")
  Error in strwidth("a") : plot.new has not been called yet

As it says, it's  plot.new(), *not* a (new) graphics window
{and that should be opened automatically when needed by all R
 level plotting functions}.


    Frank> I can sense the need for
    Frank> plot.new() in the code using if(length(dev.list()).

I don't think so.  See the mini-example above
Typically, I think you should consider using  strwidth() and strheight()
only *after* a first plot() call which sets up the coordinate
system etc.

    Frank> If using postscript( ) as the device instead of the
    Frank> screen (X11()) I need to issue plot.new even though
    Frank> dev.list() is non-null.  If I routinely call
    Frank> plot.new() I will get extra empty panels if the code
    Frank> in the calling environment included
    Frank> par(mfrow=c(#rows,#columns)).

    Frank> Does anyone know of almost foolproof code for
    Frank> initializing a new plot inside a high-level plotting
    Frank> function but not unnecessarily doing so? 

I'm pretty sure you want something like

    if(!dev.interactive()) 
       get(getOption("device"))()

Note that  help(device)  has almost this as example :

     ## open the default screen device on this platform if no
     ## device is open
     if(dev.cur() == 1) get(getOption("device"))()


But again, this does not quite answer your initial question
about ``sensing when it's safe to use strwidth()'' !

Martin
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list