#See David Williams' book "Weighing the odds", p286 y <- c(1.21, 0.51, 0.14, 1.62, -0.8, 0.72, -1.71, 0.84, 0.02, -0.12) ybar <- mean(y) ylength <- length(y) ybarv <- rep(ybar, ylength) x <- 1:ylength plot(x,y,asp=1,xlab="position",ylab="ybar",type="n",ylim=c(-1,1)) segments(x[1], ybar, x[ylength], ybar) segments(x,ybarv,x,y) points(x, ybarv, pch=21, bg="white") points(x,y,pch=19,col="black") With asp=1, the value of ylim seems to be totally ignored, as in the above code. With asp not set, R plays close attention to the value of ylim. This is not intuitive behaviour, or is it? How can I set the aspect ratio, and simultaneously set the plot region? The aspect ratio is one number and the plot region is given by four numbers (xleft, xright, yleft, yright). Logically, these 5 numbers are independent of each other and arbitrary, provided xleft<xright and yleft<yright. This should give a one-to-one bijection between 5-tuples and plots, determined up to a change of scale that is uniform in the x- and y-dirctions. My code above shows the (to me) obvious attempt, which fails. Thanks David
On 21/07/2008, at 5:27 AM, David Epstein wrote:> #See David Williams' book "Weighing the odds", p286 > > y <- c(1.21, 0.51, 0.14, 1.62, -0.8, > 0.72, -1.71, 0.84, 0.02, -0.12) > ybar <- mean(y) > ylength <- length(y) > ybarv <- rep(ybar, ylength) > x <- 1:ylength > plot(x,y,asp=1,xlab="position",ylab="ybar",type="n",ylim=c(-1,1)) > segments(x[1], ybar, x[ylength], ybar) > segments(x,ybarv,x,y) > points(x, ybarv, pch=21, bg="white") > points(x,y,pch=19,col="black") > > With asp=1, the value of ylim seems to be totally ignored, as in > the above code. With asp not set, R plays close attention to the > value of ylim. This is not intuitive behaviour, or is it? > > How can I set the aspect ratio, and simultaneously set the plot > region? The aspect ratio is one number and the plot region is given > by four numbers (xleft, xright, yleft, yright). Logically, these 5 > numbers are independent of each other and arbitrary, provided > xleft<xright and yleft<yright. This should give a one-to-one > bijection between 5-tuples and plots, determined up to a change of > scale that is uniform in the x- and y-dirctions. My code above > shows the (to me) obvious attempt, which fails.I just tried > set.seed(42) > x <- runif(10) > y <- runif(10) > plot(x,y,ylim=c(-1,2),asp=1) and it seemed to give results as expected/desired. cheers, Rolf Turner ###################################################################### Attention:\ This e-mail message is privileged and confid...{{dropped:9}}
Take a look at eqscplot() in package MASS for a different approach. You last para forgets that once you have set the device region and the margins the physical plot region and hence its aspect ratio is determined -- see the figures in 'An Introduction to R'. On Sun, 20 Jul 2008, David Epstein wrote:> #See David Williams' book "Weighing the odds", p286 > > y <- c(1.21, 0.51, 0.14, 1.62, -0.8, > 0.72, -1.71, 0.84, 0.02, -0.12) > ybar <- mean(y) > ylength <- length(y) > ybarv <- rep(ybar, ylength) > x <- 1:ylength > plot(x,y,asp=1,xlab="position",ylab="ybar",type="n",ylim=c(-1,1)) > segments(x[1], ybar, x[ylength], ybar) > segments(x,ybarv,x,y) > points(x, ybarv, pch=21, bg="white") > points(x,y,pch=19,col="black") > > With asp=1, the value of ylim seems to be totally ignored, as in the above > code. With asp not set, R plays close attention to the value of ylim. This is > not intuitive behaviour, or is it? > > How can I set the aspect ratio, and simultaneously set the plot region? The > aspect ratio is one number and the plot region is given by four numbers > (xleft, xright, yleft, yright). Logically, these 5 numbers are independent of > each other and arbitrary, provided xleft<xright and yleft<yright. This should > give a one-to-one bijection between 5-tuples and plots, determined up to a > change of scale that is uniform in the x- and y-dirctions. My code above > shows the (to me) obvious attempt, which fails. > > Thanks > David > > ______________________________________________ > 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.-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
Look at the squishplot function in the TeachingDemos package (probably not the best named function around, but somewhat descriptive and noone has suggested a better one):> x <- runif(100) > y <- runif(100) > squishplot( xlim=c(0,1), ylim=c(0.5, 0.7), asp=1 ) > plot(x,y, xlim=c(0,1), ylim=c(0.5, 0.7))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 David Epstein > Sent: Sunday, July 20, 2008 11:27 AM > To: r-help at r-project.org > Subject: [R] asp and ylim > > #See David Williams' book "Weighing the odds", p286 > > y <- c(1.21, 0.51, 0.14, 1.62, -0.8, > 0.72, -1.71, 0.84, 0.02, -0.12) ybar <- mean(y) > ylength <- length(y) ybarv <- rep(ybar, ylength) x <- 1:ylength > plot(x,y,asp=1,xlab="position",ylab="ybar",type="n",ylim=c(-1,1)) > segments(x[1], ybar, x[ylength], ybar) > segments(x,ybarv,x,y) > points(x, ybarv, pch=21, bg="white") > points(x,y,pch=19,col="black") > > With asp=1, the value of ylim seems to be totally ignored, as > in the above code. With asp not set, R plays close attention > to the value of ylim. This is not intuitive behaviour, or is it? > > How can I set the aspect ratio, and simultaneously set the > plot region? The aspect ratio is one number and the plot > region is given by four numbers (xleft, xright, yleft, > yright). Logically, these 5 numbers are independent of each > other and arbitrary, provided xleft<xright and yleft<yright. > This should give a one-to-one bijection between 5-tuples and > plots, determined up to a change of scale that is uniform in > the x- and y-dirctions. My code above shows the (to me) > obvious attempt, which fails. > > Thanks > David > > ______________________________________________ > 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. >