[R] fighting with ps.options and xlim/ylim

Marc Schwartz MSchwartz at MedAnalytics.com
Wed Jun 9 02:22:45 CEST 2004

On Tue, 2004-06-08 at 17:34, ivo welch wrote:
> sorry to impose again. 
> At the default point size, R seems very good in selecting nice xlim/ylim 
> parameters, and leaving a little bit of space at the edges of its 
> xlim/ylim.  alas, I now need to create ps graphics that can only occupy 
> a quarter of a page, so I need to blow up the text for readability.  
> Easy, I thought: make ps.options(pointsize=24).  Alas, this turns out to 
> be trickier than I thought.
> In plot, autogenerated xlim and ylim should now probably be scaled a 
> little, though more importantly, there needs to be more space at the 
> edge (e.g., if ylim=c(1,2), R seems to really draw axes from about 0.9 
> to 1.1).  if I do not increase this space, my axis label names overflow, 
> as do some text() annotations that are inside xlim/ylim, but have 
> pos=1.  (e.g., text(1,1,"something", pos=1) in the example; at standard 
> point size, this fits nicely; just no longer.)
> how to create smaller figures than full page is probably not an 
> infrequent need.  has anyone written a "smallpostscript-figures" 
> package, which sets this and perhaps other parameters?
> if not, how do I tell R that
>      * the usual space it leaves at the xlim/ylim needs to be bigger now?
>     * that I would like it to be more generous/less generous in its 
> autogeneration of good ylim/xlim default coordinates
> help appreciated.
> regards,
> /iaw


The default parameters for plots, specifically par("xaxs") and
par("yaxs") are set in such a fashion as to extend the axis ranges by 4%
(relative to the actual data ranges) in each direction. The default
setting being "r".

Try for example:

> plot(c(0, 1), c(0, 1))
> par("usr")
[1] -0.04  1.04 -0.04  1.04

par("usr") shows the current actual ranges of the plot region.

Now try:

> plot(c(0, 1), c(0, 1), xaxs = "i", yaxs = "i")
> par("usr")
[1] 0 1 0 1

The use of "i" sets the xlim and ylim values exactly to the range of the
x and y points given.

Conceptually, if you need to extend the xlim and ylim values to
something other than the exact ranges or the ranges extended by 4%, you
can use something like:

plot(x, y, xlim = range(x) * adj.x, ylim = range(y) * adj.y)

where adj.x and adj.y are adjustment factors for the axis range limits.

Example values might be:

adj.x <- c(.9, 1.1)
adj.y <- c(.8, 1.2)

which would extend the x axis by 10% and the y axis by 20%.

The exact implementation may be dependent upon the rest of your code and
other factors, but the concept remains the same.

The question I would have for you is what are you doing with the
graphics. Are you creating a 2 x 2 matrix of graphics, each one being a
quarter page, or are you simply scaling a single plot so that it would
fit to a quarter page?

If the former, you can use something like par(mfrow = c(2, 2)), which
would set up a 2 x 2 matrix within a single plotting region in the
device. If you need the overall area to be square, you could also set
par(pty = "s"). See ?par for more information and some of the examples
at the bottom of the help page.

If the latter, simply create an EPS file [see the Details section of
?postscript for the proper arguments to postscript()] that can then be
included in, for example, a LaTeX file or a word processing document
file. In this case, you can scale the size of the overall plot within
the document page, without having to worry about how large the original
plot is. That is one of the advantages of using a vector based plot
rather than a bitmap plot. They can be scaled as needed. That being
said, you might just need to play around with certain parameters like
the plot margins [ie. par("mar")], to ensure that there is enough room
for your labels and other text components. Without seeing your actual
plot, it is hard to give specific recommendations.

If you are indeed using LaTeX, you might want to review
www.ctan.org/tex-archive/info/epslatex.pdf, which provides some guidance
on how to include graphics in LaTeX files and related issues.


Marc Schwartz

More information about the R-help mailing list