[R] Properly initializing a plot

Frank E Harrell Jr fharrell at virginia.edu
Wed Sep 4 15:31:09 CEST 2002


On Wed, 4 Sep 2002 09:23:17 +0200 (CEST)
Roger Bivand <Roger.Bivand at nhh.no> wrote:

> On Wed, 4 Sep 2002, Martin Maechler wrote:
> 
> > >>>>> "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()'' !
> > 
> 
> Maybe:
> 
> > aa <- try(strwidth("a"))
> Error in strwidth("a") : plot.new has not been called yet
> > class(aa)
> [1] "try-error"
> 
> or:
> 
> > options("show.error.messages"=FALSE)
> > aa <- try(strwidth("a"))
> > class(aa)
> [1] "try-error"
> 
> Roger
> 
> -- 
> Roger Bivand
> Economic Geography Section, Department of Economics, Norwegian School of
> Economics and Business Administration, Breiviksveien 40, N-5045 Bergen,
> Norway. voice: +47 55 95 93 55; fax +47 55 95 93 93
> e-mail: Roger.Bivand at nhh.no
> and: Department of Geography and Regional Development, University of
> Gdansk, al. Mar. J. Pilsudskiego 46, PL-81 378 Gdynia, Poland.
> 

Thanks very much for your responses Martin and Roger.  I'm still struggling with this because I can't invoke plot( ) before the margins are computed (unless I draw an empty plot I guess), and I'd lean against triggering an error even if the error can be covered up.  Perhaps a general solution is to, in a high-level function that needs to call strwidth before plot( ), always do the following:

plot.new()
strwidth(..., units='inches')
par(new=TRUE)
par(mai=...)
plot( ... )

Make sense?

Frank
-- 
Frank E Harrell Jr              Prof. of Biostatistics & Statistics
Div. of Biostatistics & Epidem. Dept. of Health Evaluation Sciences
U. Virginia School of Medicine  http://hesweb1.med.virginia.edu/biostat
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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