[R] plot scale

Greg Snow Greg.Snow at imail.org
Fri Oct 2 21:40:30 CEST 2009


Here is a different approach:
This example uses the default plotting device, but should work the same with postscript or any other device.  Just set the size of the paper to a standard size, or a large enough size to fit your largest plot:

dev.new()
tmp <- par('plt')

scale <- 5
x <- runif(100, 0, 20)
y <- rnorm(100, x)

dx <- diff(range(x))*1.08
wx <- grconvertX(dx/scale, from='inches', to='ndc')

dy <- diff(range(y))*1.08
wy <- grconvertY(dy/scale, from='inches', to='ndc')

par(plt = c(tmp[1], tmp[1]+wx, tmp[3], tmp[3]+wy) )
plot(x,y)

dev.new()

x2 <- runif(100, 0,5)
y2 <- rnorm(100, x2)

dx <- diff(range(x2))*1.08
wx <- grconvertX(dx/scale, from='inches', to='ndc')

dy <- diff(range(y2))*1.08
wy <- grconvertY(dy/scale, from='inches', to='ndc')

par(plt = c(tmp[1], tmp[1]+wx, tmp[3], tmp[3]+wy) )
plot(x2,y2)


The above only works for 1 plot per page (no layout or par(mfrow=... ) ).

This keeps the lower left corner of the plot in the same place (so change the margins before the call to par('plt') if you want them different). Then sets the top and right parts of the plotting region leaving white space on the top and right to get the scaling correct (you will have problems if your desired scale does not fit in the window/on the page).  This example uses a "scale" of 5 plot units per inch, change as appropriate.  The *1.08 is to account for the extra area that is added to the plot by default, change this for the other axis type.

Hope this helps,



-- 
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.snow at imail.org
801.408.8111


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Ben Kenward
> Sent: Friday, October 02, 2009 2:08 AM
> To: r-help at r-project.org
> Subject: [R] plot scale
> 
> Hi,
> 
> Is there a way to set the scale of a plot (i.e. number of axis units
> per centimeter) when you output it to postscript? If not, how am I
> supposed to plot graphs with different axis limits to the same scale?
> They just get resized to fit the paper so that graphs which show a
> smaller number of axis units end up with a larger scale.
> 
> Cheers,
> 
> Ben
> 
> --
> Dr. Ben Kenward
> Department of Psychology, Uppsala University, Sweden
> http://www.benkenward.com
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-
> guide.html
> and provide commented, minimal, self-contained, reproducible code.




More information about the R-help mailing list