[R] Arguments to "personalised" plot()

(Ted Harding) Ted.Harding at manchester.ac.uk
Sun Oct 7 21:49:30 CEST 2007


Hi Folks,

I'm curious for an explanation of the following -- it's a
matter of trying to understand how R parses it.

I've written sundry little "helper" variants of functions,
in particular plot(), to save repetitively typing the same
options over and over again.

For example:

plotb <- function(x,...){plot(x,pch="+",col="blue",...)}

This does exactly what you'd expect it to do when fed with
a vector of values to plot, e.g.

  plotb(cos(0.01*2*pi*(0:100)))

namely a plot of the values of cos(..) with x-coordinates
marked 0, 20, 40, 60, 80, 100, as blue "+".

As expected, one can add other plot options if needed, e.g.

  plotb(cos(0.01*2*pi*(0:100)), xlim=c(0,4*pi))

if one wants. In this case, I'm supposing that the "xlim=c(0,4*pi)"
goes in under the umbrella of "...", which is what I guessed
would happen.

Interestingly, though, if I do

  x<-0.01*2*pi*(0:100); plotb(x,cos(x))

I now get it with the x-axis labelled 0,1,2,3,4,5,6 just as
if I had used the built-in

  x<-0.01*2*pi*(0:100); plot(x,cos(x),pch="+",col="blue")

and I can *also* add "xlim=c(0,4*pi)":

  x<-0.01*2*pi*(0:100); plotb(x,cos(x),xlim=c(0,4*pi))

and it still works! Now the latter is the same "..." mechanism
as before, I suppose; but this doesn't explain how plotb()
"sees" x, along with cos(x), and picks it up to do the
right thing.

So my question -- which is why I'm posting -- is:

How does "x" get in along with "cos(x)" when I do
"plotb(x,cos(x))", when the definition of the function is

  plotb <- function(x,...){plot(x,pch="+",col="blue",...)}

Thanks, and best wishes to all,
Ted.

--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 07-Oct-07                                       Time: 20:49:26
------------------------------ XFMail ------------------------------



More information about the R-help mailing list