Wandering usr values in par(no.readonly=TRUW) (PR#2283)

Marc Schwartz mschwartz@medanalytics.com
Tue, 12 Nov 2002 15:51:17 -0600


> -----Original Message-----
> jarioksa@sun3.oulu.fi
> 
> Dear R folks,
> 
> Initially I had a plotting routine using logarithmic y-axes
> that failed after repeated calls if I tried to restore the 
> graphical parameters (which I wanted to do because I used 
> `layout' within the routine. I tried to isolate the problem 
> and found out that the following code with logarithmic axis 
> is sufficient for reproducing the failure:
> 
> > plot2.log
> function (x=1:2,y=1:2)
> {
>    op <- par(no.readonly=TRUE)
>    on.exit(par(op))
>    cat(op$usr, "\n")
>    plot(x, y, log="y")
>    #par(op)
>    invisible(op)
> }
> 
> Here is a session with the typical error message (Infinite
> axis extents):
> 
> > op <- par(no.readonly=TRUE)
> > op$usr
> [1] 0 1 0 1
> > plot2.log()$usr
> 0 1 0 1
> [1] 0 1 0 1
> > plot2.log()$usr
> 0 1 1 10
> [1]  0  1  1 10
> > plot2.log()$usr
> 0 1 10 1e+10
> Error in par(op) : Infinite axis extents [GPretty(1e+10,inf,5)]
> 
> So this happens only with a repeated call to logarithmic axes.
> 
> I found out this feature sometimes back in R-1.5*, but I
> regarded that as my 
> own bug. It started to disturb me yesterday when I had a live 
> R session in my 
> lectures, and got to edit away the par-restoring code to continue.
> 
> Best wishes, Jari Oksanen

This works outside of the function as well by repeating the following 6
lines, 3 times:

op <- par(no.readonly=TRUE)
plot(1:2, 1:2, log = "y")
par(op)
# take note of the following values as they change with each iteration
op$usr
par("usr")

However, if you close the plot window before each subsequent iteration,
it does not occur and you can repeat ad nauseaum, presumably because a
fresh plotting device with the default pars is being created each time.

It would appear at first glance that the values being returned to
par("usr") from op$usr after the plot() with a log axis are the "log
axis transformed" par("usr") values.

That is par("usr") starts as 0, 1, 0, 1 before plotting. 

After the first log plot, the restored par("usr") is now 0, 1, 1, 10. 

At that point, par("usr")[3:4] which are the y-axis related values, are
10 ^ 0 and 10 ^ 1 respectively, which is consistent with the way that
the par("usr") values are transformed when using log scaling.

This continues on the second iteration as the starting values for
par("usr")[3:4] of 1 and 10 after being restored are now transformed
into 10 ^ 1 and 10 ^ 10 respectively.

It would seem that upon the restoration of the initial stored par
values, the values are being restored as if they are exponents.

BTW, this occurs with log = "x" and log = "xy" as well, thus affecting
the first two and all four par("usr") values, respectively, in a similar
fashion.

I am guessing that this may be the result of par("ylog") and par("xlog")
being read only and perhaps impacting the restoration of the par("usr")
values when the plotting device is still open, but that may be
incorrect.

HTH a little,

Marc Schwartz


-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-devel 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-devel-request@stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._