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

From: Ted Harding <Ted.Harding_at_nessie.mcc.ac.uk>
Date: Wed 13 Jul 2005 - 20:01:42 EST


On 13-Jul-05 Prof Brian Ripley wrote:

> For most purposes it is easiest to use matplot() to plot superimposed 
> plots like this.  E.g.
> 
> x <- 0.1*(0:20)
> matplot(x, cbind(sin(x), cos(x)), "pl", pch=1)

This, and Robin's suggestion, are good practical solutions especially when only a few graphs (2 or 3 or ... ) are involved. However, their undelying principle is to accumulate auxiliary variables encapsulating the graphs which will eventually be plotted.

However, once in a while I like to make a really messy graph of superimposed sample paths of a simulated stochastic process, perhaps with several dozen replications and many points (even 5000) along each sample path. An example where this has a real practical point is diffusion from the chimney stack of, say, an incinerator. The resulting plot can give a good picture of the "average plume", allowing the viewer to form an impression of the variation in concentration along and on the fringes of the plume.

This is definitely a case where "dynamic rescaling" could save hassle! Brian Ripley's suggestion involves first building a matrix whose columns are the replications and rows the time-points, and Robin Hankin's could be easily adapted to do the same, though I think would involve a loop over columns and some very long vectors.

How much easier it would be with dynamic scaling!

Best wishes,
Ted.

> On Wed, 13 Jul 2005, Robin Hankin wrote:
> 

>> Hi
>>
>> Ted makes a good point... matlab can dynamically rescale a plot in
>> response
>> to plot(...,add=TRUE) statements.
>>
>> For some reason which I do not understand, the rescaling issue is
>> only a problem
>> for me when working in "matlab mode". It's not an issue when working
>> in "R mode"
>>
>> Ted pointed out that the following does not behave as intended:
>>
>>
>>> x = 0.1*(0:20);
>>> plot(x,sin(x))
>>> lines(x,1.5*cos(x))
>>
>>
>> and presented an alternative method in which ylim was set by hand. I
>> would suggest:
>>
>> x <- 0.1*(0:20)
>> y1 <- sin(x)
>> y2 <- 1.5*cos(x)
>>
>> plot(c(x,x),c(y1,y2),type="n")
>> lines(x,y1)
>> lines(x,y2)
>>
>> because this way, the axes are set by the plot() statement, but
>> nothing is plotted.
>>
>>
>> best wishes
>>
>> rksh
>>
>>
>>
>>
>>
>> On 13 Jul 2005, at 09:12, (Ted Harding) wrote:
>>>>
>>>
>>> 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@nessie.mcc.ac.uk>
>>> Fax-to-email: +44 (0)870 094 0861
>>> Date: 13-Jul-05 Time: 09:12:34
>>> ------------------------------ XFMail ------------------------------
>>>
>>> ______________________________________________
>>> 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
>>>
>>
>> --
>> Robin Hankin
>> Uncertainty Analyst
>> National Oceanography Centre, Southampton
>> European Way, Southampton SO14 3ZH, UK
>> tel 023-8059-7743
>>
>> ______________________________________________
>> 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
>>
> 
> -- 
> Brian D. Ripley,                  ripley@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
> 
> ______________________________________________
> 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

--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding@nessie.mcc.ac.uk> Fax-to-email: +44 (0)870 094 0861
Date: 13-Jul-05                                       Time: 11:00:01
------------------------------ XFMail ------------------------------

______________________________________________
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 Wed Jul 13 20:39:32 2005

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