[R] Aspect ratio and limits
Marc Schwartz
MSchwartz at MedAnalytics.com
Tue Apr 19 18:17:27 CEST 2005
On Tue, 2005-04-19 at 16:28 +0100, Barry Rowlingson wrote:
> Suppose I have the following data I want to scatterplot:
>
> > xy
> [,1] [,2]
> [1,] 0 0
> [2,] 21 4
>
> I start up a graphics window and fire away:
>
> > plot(xy)
>
> - but because the graphics window is square, the aspect ratio is
> wrong. So I add:
>
> > plot(xy, asp=1)
>
> - now the aspect ratio is correct, but the Y range is about -8 to 11,
> whereas my data has a Y range of 0 to 4. The plot appears in the middle
> of a mostly empty square. So lets try:
>
> > plot(xy, asp=1, ylim=c(0,4))
>
> - which seemingly changes nothing. The reason being that par()$pty is
> 'm', which means to use as much of the plot area as possible. The only
> other option is 's' which produces a square plot. I want it to produce a
> very rectangular plot. R cant comply with all these requests.
>
> If I leave par(pty='m') then I can change the shape of the graphics
> window until I get the effect I want, but this seems an unsatisfactory
> way of doing it, and when I come to make a PostScript version I need to
> set the dimensions of the PS device correctly.
>
> Is there a right way to do this? The only way I can think is to do the
> plot without box and axes, and then add them afterwards, but that could
> get very messy. Have I missed something obvious?
Here is one possibility that could be used for both your X11 and
postcript devices. Using your small dataset 'xy' for this example:
xy <- matrix(c(0, 21, 0, 4), ncol = 2)
> xy
[,1] [,2]
[1,] 0 0
[2,] 21 4
# Get ratio of width to height
aspect <- diff(range(xy[, 1])) / diff(range(xy[, 2]))
# Set height value as you require
height <- 3
X11(width = aspect * height, height = height)
# Now plot
plot(xy, asp = 1)
#For postscript
height <- 3
postscript(width = aspect * height, height = height,
onefile = FALSE, paper = "special",
horizontal = FALSE)
plot(xy, asp = 1)
dev.off()
You can of course, adjust the height value to your desire or reverse the
computation to allow you to specify the width, etc.
Does that get you what you need?
HTH,
Marc Schwartz
More information about the R-help
mailing list