[R] R plot

Greg Snow Greg.Snow at imail.org
Fri Oct 17 19:14:14 CEST 2008


There are some functions that will do this in 1 step, like matplot if the the data is formatted properly.  ggplot has already been mentioned as another approach.

If you want to stick with base graphics, there is a zoomplot function in the TeachingDemos package that will rescale a plot (zoom in or out) and works in this case:

> library(TeachingDemos)
>
> a<-rnorm(5)
> b<-rnorm(5)
> plot(a,b,col = "red")
> points(10,-10)
>
> zoomplot( range(a,10), range(b,-10) )
>

If you want this a little more automatic, you can easily write a function which does the zooming and plotting both,  a simple version of the function would be:


> expandpoints <- function(x, y, ... ) {
+ usr <- par('usr')
+ zoomplot( range(x,usr[1:2]), range(y,usr[3:4]) )
+ points(x,y,...)
+ }
>
> plot(a,b,col="red")
> expandpoints(10,-10)
>

If you are going to use this much, it should be modified to accept the same types of input as points and possibly do some other checking.  Note also that this will be adding a little extra area around the old points as well unless the axis style is changed.

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 Haoda Fu
> Sent: Thursday, October 16, 2008 10:07 PM
> To: r-help at r-project.org
> Subject: [R] R plot
>
> All -
>
>
> When I plot something like
>
> a<-rnorm(5)
> b<-rnorm(5)
> plot(a,b,col = "red")
> points(10,-10)
>
> The last point is missing because it is out of
> range of the first plot.
>
> I just try to switch from Matlab to R. In Matlab,
> it always can automatic adjust the xlim and ylim
> for such case.
>
> Is it possible auto adjust in R? Otherwise
> keep tracking xlim and ylim is really annoying.
>
> Best,
> Haoda
>
> ______________________________________________
> 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