[R] Properly initializing a plot

Torsten Hothorn Torsten.Hothorn at rzmail.uni-erlangen.de
Wed Sep 4 16:20:26 CEST 2002


> 
> 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?
> 

I had the same problem (see r-devel "strwidth" 3 weeks ago) and Brian
pointed to the following solution which is very close to your example
above: 

  # open a new device 
  plot.new()
  # get all possible parameters for `par' from the function call 
  args <- list(...)
  # check if the size of the axis needs to be set
  cex.axis <- args$cex.axis
  if (!is.null(cex.axis))
    par(cex.axis=cex.axis)
  # we need to determine the left margin depending on the size of the
  # characters in rownames(est) <- this is what I want to plot
  # save the old ones
  oldmai <- mymai <- par("mai")
  # determine the size needed for plotting
  ywidth <- max(strwidth(rownames(est), units="inches",
                         cex=par("cex.axis")))*1.2
  # if there is not enough room enlarge it 
  if (mymai[2] < ywidth)
   mymai[2] <- ywidth
  # tell par what we want 
  par(mai=mymai, new=TRUE)
  # set up an empty plot with appropriate coordinate system
  plot(rbind(c(crange[1], 1), c(crange[2], n)), type="n", axes=FALSE,
       xlab=xlab, ylab="", main=type, ...)
  # and do magic things ...
  axis(1, ...)
  axis(2, 1:n, rownames(est)[n:1], las=1, ...)
  ...
 
Torsten

> 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
> _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
> 

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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