Re: [R] missed ylim from plot.default

From: Greg Snow <Greg.Snow_at_intermountainmail.org>
Date: Sat 27 May 2006 - 05:35:48 EST


Try the following function to see if it does what you want. The basic syntax for your example would be:

> tmp <- squishplot( xlim=c(-0.5,7), ylim=c(-0.5,2.8), asp=1 )
> plot(1:5, rep(1,5), xlim=c(-0.5,7), ylim=c(-0.5,2.8))
> par(tmp) # reset plotting region for future plots

The function definition is:

squishplot <- function(xlim,ylim,asp=1){   if(length(xlim) < 2) stop('xlim must be a vector of length 2')   if(length(ylim) < 2) stop('ylim must be a vector of length 2')

  tmp <- par(c('plt','pin','xaxs','yaxs'))

  if( tmp$xaxs == 'i' ){ # not extended axis range

        xlim <- range(xlim)
  } else { # extended range

	tmp.r <- diff(range(xlim))
	xlim <- range(xlim) + c(-1,1)*0.04*tmp.r
  }

  if( tmp$yaxs == 'i' ){ # not extended axis range

        ylim <- range(ylim)
  } else { # extended range

	tmp.r <- diff(range(ylim))
	ylim <- range(ylim) + c(-1,1)*0.04*tmp.r
  }

  tmp2 <- (ylim[2]-ylim[1])/(xlim[2]-xlim[1])

  tmp.y <- tmp$pin[1] * tmp2 * asp

  if(tmp.y < tmp$pin[2]){ # squish vertically
	par(pin=c(tmp$pin[1], tmp.y))
	par(plt=c(tmp$plt[1:2], par('plt')[3:4]))
  } else { # squish horizontally
	tmp.x <- tmp$pin[2]/tmp2/asp
	par(pin=c(tmp.x, tmp$pin[2]))
	par(plt=c(par('plt')[1:2], tmp$plt[3:4]))
  }

  return(invisible(tmp['plt']))
}

Let me know of any improvments you can think of (including a better name) and how it works.

-- 
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.snow@intermountainmail.org
(801) 408-8111
 

-----Original Message-----
From: r-help-bounces@stat.math.ethz.ch
[mailto:r-help-bounces@stat.math.ethz.ch] On Behalf Of Antonio, Fabio Di
Narzo
Sent: Friday, May 26, 2006 1:29 AM
To: Peter Ehlers
Cc: R-help@stat.math.ethz.ch
Subject: Re: [R] missed ylim from plot.default

2006/5/26, Peter Ehlers <ehlers@math.ucalgary.ca>:
>
>

> Antonio, Fabio Di Narzo wrote:
>
> > Hi all.
> > On my R-2.3.0, calling:
> >
> >
> >>plot(1:5, rep(1,5), xlim=c(-0.5,7), ylim=c(-0.5,2.8), asp=1)
> >
> >
> > gives to me a plot (tried pdf and X11) whose y axis goes from about
> > -2
> to
> > about 4.5. What I've missed? How can I show some pre-specified x and

> > y ranges from a plot (keeping fixed the aspect ratio)?
> >
> > Tnx all,
> > Antonio, Fabio Di Narzo.
> >
> > [[alternative HTML version deleted]]
>
> Do you want a wide, but not very tall plot? If so, you could specify
> the width/height in your X11() call.
>
> Peter Ehlers
> > Tnx for your indication, now I see: x and y plotting ranges are influenced by the actual window size (at least when fixing aspect ratio). I think this is obvious... What I missed/forgot above, was that default plot width and height are fixed indipendently from the 'x-ylim' and 'asp' arguments. However now I have a problem: I can write a wrapper to set device width and height depending on the (xlim,ylim,asp) triple. But there's often extra-space in the device due to plot title, axis labels, etc. So, I fear I can't simply set width=height*asp. Some suggestion? (Anyway, by now I only have to produce few plots, so today I can go try by try :-) ). Bests, Antonio. [[alternative HTML version deleted]] ______________________________________________ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html ______________________________________________ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Received on Sat May 27 05:42:14 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Sat 27 May 2006 - 08:10:26 EST.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.