[R] Problems when plotting (related to RArcInfo)

Paul Murrell p.murrell at auckland.ac.nz
Thu Nov 14 23:52:32 CET 2002


Virgilio Gómez Rubio wrote:
> Hello,
> I am using RArcInfo to create some maps but, once I have plotted the map
> (using plotarc), I try to add new lines, but the fact is that they are
> not plotted.
> I have reviewing the code, and if I remove a line, everything works
> fine, but I am not sure whether I should do that, because that line
> restores the previous 'par' profile.
> Here is a portion of the code (from plotarc.R):
>                 par.in <- par(no.readonly = TRUE)
> #               on.exit(par(par.in))  <--- THIS IS THE LINE I MENTIONED
>                 plot.dim<-c(ymin-xmin, ymax-xmax)
>                 par(pin = min(par.in$pin)
>                 * par.in$fin / max(par.in$fin)
>                 * (plot.dim) / max(plot.dim))
> I am not sure whether some 'read only' flag is set with that command.
> can anyone help.

The result of the on.exit(par(par.in)) is to reset (most) graphics
parameters on the device, INCLUDING the plotting coordinate system. 
This is why your extra lines do not appear in normal usage.

This is the approach that some complex plotting functions take because
they produce a very complex and/or multiple-plot arrangement and it
would be ambiguous where annotations should be added subsequently (e.g.,
coplot, filled.contour). Whenever a plotting function uses this
approach, the plot becomes "closed" -- that is, it is not possible to
annotate the plot like usual with subsequent calls to lines, points, and

One way around this is to provide an argument to the plotting function
that allows the user to specify a "panel" function;  the panel function
is used to do annotation -- it gets called within the plotting function
where the correct coordinate systems are available.

The following example may help show the problem and solution:

    closed.plot <- function(x, y, panel=NULL, ...) {
      opar <- par(no.readonly=TRUE)
      plot(x, y, xlim=c(0, 100), ...)
      title(main="Closed Plot", adj=1, line=0)
      if (!is.null(panel))
    par(mfrow=c(2, 1))
    plot(1:10, 1:10)
    title(main="Previous Plot", adj=1, line=0)
    axis(1, at=5, col="red", col.axis="red")
    closed.plot(1:10, 1:10)
    # annotate outside "closed" plot
    abline(h=5, lty="dotted", col="red")
    abline(v=5, lty="dotted", col="red", xpd=NA)
    text(5, 5, "What am I doing here?", col="red", adj=c(0, 0))
    axis(2, at=5, col="red", col.axis="red")
    par(mfrow=c(2, 1))
    plot(1:10, 1:10)
    title(main="Previous Plot", adj=1, line=0)
    # annotate "closed" plot via panel function
    closed.plot(1:10, 1:10,
                panel={ abline(v=5, h=5, lty="dotted", col="green");
                        text(5, 5, "That's better!", col="green",
                             adj=c(0, 0));
                        axis(1, at=5, col="green", col.axis="green");
                        axis(2, at=5, col="green", col.axis="green") })
Maybe you could encourage the RArcInfo authors to add a "panel"
Alternatively, you could encourage the RArcInfo authors to remove the
on.exit() call as you did.  At a quick glance, it does not appear that
there is a great need for it -- but I could be incredibly wrong there so
it's probably best to check with the authors :)

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