[R] par("usr") trouble in multiplot axis scaling

Marc Schwartz MSchwartz at MedAnalytics.com
Mon Oct 25 21:25:55 CEST 2004


On Mon, 2004-10-25 at 13:19, Johannes Graumann wrote:
> Hello,
> 
> I'm blotting a series of growth curves into a multiplot environment
> created with layout().
> since I want the four plots to be easily visually comparable, I do the
> following:
> 
> #first plot
> plot(x,y,<stuff>)
> standarduser<-par()$usr
> ...
> <some fitting>
> ...
> lines(spline(x, <fitted_equation>))
> 
> #everything all right till here
> # second plot
> plot(x,y,<stuff>)
> par(usr=standarduser)
> ...
> <some fitting>
> ...
> lines(spline(x, <fitted_equation>))
> 
> The problem here is, that the axis of the second plot seem to be scaled
> according to the parameters of the first, BUT the fitted curve in the
> second plot isn't!
> 
> Any idea about what I'm doing wrong?
> 
> Please help this newbie out of his misery!
> 
> Joh

If I am correctly understanding what you are doing and what you want,
you would like each of the four plots to have the same axis ranges?

Part of the problem, I think, is that in your second plot(), the axis
ranges are automatically set based upon the ranges of your x and y data
in that call. These presumably are different than the x and y values in
your first plot? 

Thus, the initial plot region scales are going to be different for each
plot. By default, this will be range(x) +/- 4% and range(y) +/- 4%.

When you force the second plot region's values to be 'standarduser',
your underlying x,y plot, having already been drawn, and the new lines
to be added are then on different scales in the same plot.

If my assumptions are correct, you would be better off calling plot()
each time using the 'xlim' and 'ylim' arguments to explicitly define the
axis ranges with known common values.

For example, if you know that the range of all x values is r.x and the
range of all y values is r.y:

#first plot
plot(x, y, <stuff>, xlim = r.x, ylim = r.y)
...
<some fitting>
...
lines(spline(x, <fitted_equation>))


# second plot
plot(x, y, <stuff>, xlim = r.x, ylim = r.y)
...
<some fitting>
...
lines(spline(x, <fitted_equation>))


This gets around the need to manipulate the pars directly and hopefully
less confusion in reading the code. The key is knowing the common ranges
of your x and y values in advance.

Does that help?

Marc Schwartz




More information about the R-help mailing list