Re: [R] How to use the function "plot" as Matlab?

From: Greg Snow <>
Date: Thu 14 Jul 2005 - 03:12:21 EST

>>> Ted Harding <> 07/13/05 02:12AM >>>


>> I'm not sufficiently acquainted with the internals of "plot"
>> and friends to anticipate the answer to this question; but,
>> anyway, the question is:
>> Is it feasible to include, as a parameter to "plot", "lines"
>> and "points",
>> rescale=FALSE
>> where this default value would maintain the existing behaviour
>> of these functions, while setting
>> rescale=TRUE
>> would allow each succeeding plot, adding graphs using "points"
>> or "lines", to be rescaled (as in Matlab/Octave) so as to
>> include the entirety of each successive graph?

I tried editing the range in the result from "recordPlot" and it crashed
R on my system, so it probably is not trivial to rescale an existing plot
on the standard devices. Part of the issue is what information is saved
when the plot is made and what is recomputed each time. Apparently octave/matlab and R do this quite differently.

Others have suggested using matplot. you can also manually save all the relevent information yourself to redo the plots.

The other option is to use a different graphics device that supports rescaling. One option is "rgl" using the rgl package and the rgl.lines function (it will auto rescale, but seems overkill for this case).

Another option is to go a similar route to octave and to have gnuplot do the actual plotting (and keep the info to rescale when needed).

Below are some functions I wrote for passing the data to gnuplot (I am working on windows and downloaded the win32 version of gnuplot from Some editing may be neccessary for these to work on other systems. If there is interest I may debug and expand these functions and include them in a package.

To do the original example with these functions:
x <- seq(0,2,0.1)

gp.plot(x,sin(x), type='l')
gp.plot(x,1.5*cos(x), type='l', add=T)
gp.send('set yrange [-1.6:1.6]') # force my own range
gp.send('set yrange [*:*]') # return to autoscaling

The function definitions are: <- function(where='c:/progra~1/GnuPlot/bin/pgnuplot.exe'){

	.gp <<- pipe(where,'w')
	.gp.tempfiles <<- character(0)


gp.close <- function({



gp.send <- function(cmd='replot',{

	cat(cmd, file=pipe)


gp.plot <- function(x,y,type='p',add=F, title=deparse(substitute(y)),{
	tmp <- tempfile()
	.gp.tempfiles <<- c(.gp.tempfiles, tmp)

	write.table( cbind(x,y), tmp, row.names=FALSE, col.names=FALSE
	w <- ifelse(type=='p', 'points', 'lines')
	r <- ifelse(add, 'replot', 'plot')

	cat( paste(r," '",tmp,"' with ",w," title


Hope this helps,

Greg Snow, Ph.D.
Statistical Data Center, LDS Hospital
Intermountain Health Care
(801) 408-8111 mailing list PLEASE do read the posting guide! Received on Thu Jul 14 03:19:13 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:33:38 EST