Hello, How to use the function plot to produce graphs as Matlab? example in Matlab: a = [1,2,5,3,6,8,1,7]; b = [1,7,2,9,2,3,4,5]; plot(a,'b') hold plot(b,'r') How to make the same in R-package ? I am trying something thus: a <- c(1,2,5,3,6,8,1,7) c(1,7,2,9,2,3,4,5) -> b a;b plot(a,t="l",col="blue") plot(b,t="l",col="red")
On 7/12/05, klebyn <klebyn at yahoo.com.br> wrote:> Hello, > > How to use the function plot to produce graphs as Matlab? > example in Matlab: > > a = [1,2,5,3,6,8,1,7]; > b = [1,7,2,9,2,3,4,5]; > plot(a,'b') > hold > plot(b,'r') > > > How to make the same in R-package ? > > I am trying something thus: > > a <- c(1,2,5,3,6,8,1,7) > c(1,7,2,9,2,3,4,5) -> b > > a;b > > plot(a,t="l",col="blue") > plot(b,t="l",col="red") >In addition to the Intro manual and other usual sources (see the posting guide at the end of every post to this list) see: http://cran.r-project.org/doc/contrib/R-and-octave-2.txt
On 13-Jul-05 klebyn wrote:> Hello, > > How to use the function plot to produce graphs as Matlab? > example in Matlab: > > a = [1,2,5,3,6,8,1,7]; > b = [1,7,2,9,2,3,4,5]; > plot(a,'b') > hold > plot(b,'r') > > > How to make the same in R-package ? > > I am trying something thus: > > a <- c(1,2,5,3,6,8,1,7) > c(1,7,2,9,2,3,4,5) -> b > > a;b > > plot(a,t="l",col="blue") > plot(b,t="l",col="red")Although this is an over-worked query -- for which an answer, given that t="l" has been specified, is to use plot(a,t="l",col="blue",ylim=c(0,10)) lines(b,t="l",col="red") there is a more interesting issue associated with it (given that Klebyn has come to it from a Matlab perspective). It's a long time since I used real Matlab, but I'll illustrate with octave which, in this respect, should be identical to Matlab. Octave: octave:1> x = 0.1*(0:20); octave:2> plot(x,sin(x)) produces a graph of sin(x) with the y-axis scaled from 0 to 1.0 Next: octave:3> hold on octave:4> plot(x,1.5*cos(x)) superimposes a graph of 1.5*cos(x) with the y-axis automatically re-scaled from -1 to 1.5. This would not have happened in R with x = 0.1*(0:20); plot(x,sin(x)) lines(x,1.5*cos(x)) where the 0 to 1.0 scaling of the first plot would be kept for the second, in which therefore part of the additional graph of 1.5*cos(x) would be "outside the box". No doubt like many others, I've been caught on the wrong foot by this more than a few times. The solution, of course (as illustrated in the reply to Klebyn above) is to anticipate what scaling you will need for all the graphs you intend to put on the same plot, and set up the scalings at the time of the first one using the options "xlim" and "ylim", e.g.: x = 0.1*(0:20); plot(x,sin(x),ylim=c(-1,1.5)) lines(x,1.5*cos(x)) This is not always feasible, and indeed should not be expected to be feasible since part of the reason for using software like R in the first place is to compute what you do not know! Indeed, R will not allow you to use "xlim" or "ylim" once the first plot has been drawn. So in such cases I end up making a note (either on paper or, when I do really serious planning, in auxiliary variables) of the min's and max's for each graph, and then re-run the plotting commands with appropriate "xlim" and "ylim" scaling set up in the first plot so as to include all the subsequent graphs in entirety. (Even this strategy can be defeated if the succesive graphs represent simulations of long-tailed distributions. Unless of course I'm sufficiently alert to set the RNG seed first as well ... ) 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? Best wishes to all, Ted. -------------------------------------------------------------------- E-Mail: (Ted Harding) <Ted.Harding at nessie.mcc.ac.uk> Fax-to-email: +44 (0)870 094 0861 Date: 13-Jul-05 Time: 09:12:34 ------------------------------ XFMail ------------------------------
>>> Ted Harding <Ted.Harding at nessie.mcc.ac.uk> 07/13/05 02:12AM >>>[snip]>> 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 http://www.gnuplot.info). 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: gp.open() 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() gp.send('set yrange [*:*]') # return to autoscaling gp.send() gp.close() The function definitions are: gp.open <- function(where='c:/progra~1/GnuPlot/bin/pgnuplot.exe'){ .gp <<- pipe(where,'w') .gp.tempfiles <<- character(0) invisible(.gp) } gp.close <- function(pipe=.gp){ cat("quit\n",file=pipe) close(pipe) if(exists('.gp.tempfiles')){ unlink(.gp.tempfiles) rm(.gp.tempfiles,pos=1) } rm(.gp,pos=1) invisible() } gp.send <- function(cmd='replot',pipe=.gp){ cat(cmd, file=pipe) cat("\n",file=pipe) invisible() } gp.plot <- function(x,y,type='p',add=F, title=deparse(substitute(y)), pipe=.gp){ 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 '",title,"'\n",sep=''), file=pipe) invisible() } Hope this helps, Greg Snow, Ph.D. Statistical Data Center, LDS Hospital Intermountain Health Care greg.snow at ihc.com (801) 408-8111