[Rd] Overriding axis formatting with custom Axis method, Axis.numeric etc

Prof Brian Ripley ripley at stats.ox.ac.uk
Mon Apr 7 15:23:15 CEST 2008

On Mon, 7 Apr 2008, Sklyar, Oleg (MI London) wrote:

> Dear Prof Ripley,
> thank you for your suggestions, they are as always very insightful!
> Although, anybody can miss a point or two as I just did in namespaces.
> Is the following correct?
> Until redefined in the current namespace, plot.default (and all other
> plot.* methods) being defined in 'graphics' calls Axis.default also
> defined in 'graphics' and it is pointless to redefine Axis.default
> without redefining plot.default in the same namespace? It worked for me
> with Axis.numeric simply because the latter matches better than
> Axis.default and it is not defined in 'graphics'.

Where S3 methods are looked for is quite subtle:

> rm(Axis.numeric)
> Axis.default <- AxisFUN
> plot.default <- plot.default
> environment(plot.default) <- .GlobalEnv
> plot(1:5, 1:5)

will use the local Axis.default.  So although Axis() is defined in the 
graphics namespace, which Axis.default is called depends on where it is 
called from.  From the help:

      'UseMethod' and 'NextMethod' search for methods in
      two places: first in the environment in which the generic function
      is called, and then in the registration data base for the
      environment in which the generic is defined (typically a name

I am really not sure what you are trying to achieve -- if it is to change 
what existing plot() methods in R do, that is quite deliberately hard.
The easiest way to do this is to change the R sources and re-install R.

> So the question is: is there any obvious way around to redefine axis
> formatting to be used with all plot.* methods in the current namespace
> avoiding duplicating all the plot.* methods along with writing new
> Axis.* methods?
> Another understanding issue: it looks like I do really need to import
> all plot methods, copy and paste the code for them and export them again
> to make the whole idea work. I.e. it is not sufficient to simply
> importFrom(graphics, plot.default); define Axis.default;
> export(plot.default, Axis.default) in order to make it work. It is also
> not sufficient to have something like plot.default =
> graphics::plot.default in package code. In fact, to make it work I had
> to copy and paste the full implementation of plot.default (although
> without altering a single character). Am I missing something obvious?

Perhaps that if you re-assign a function you do not copy it (copying is 
lazy) nor do you change its environment.  See my little example.

>> Axis() is an S3 generic, not an S4 generic, so it does not
>> make sense to me to talk about 'signatures'.  It is in the
>> graphics namespace, so you cannot redefine it for use by plot.default.
> Well, I would assume that default, POSIXct, POSIXlt and Date are kind of
> S3 signatures for Axis (Axis.*) But thanks for hinting at "methods",
> this was indeed what I needed.

'S3 signature' is not a term that I have ever seen used.

> Regards,
> Oleg
>> sessionInfo()
> R version 2.6.2 (2008-02-08)
> x86_64-unknown-linux-gnu
> locale:
> ON=C
> attached base packages:
> [1] stats     graphics  grDevices utils     datasets  methods   base
> loaded via a namespace (and not attached):
> [1] rcompgen_0.1-17

Brian D. Ripley,                  ripley at 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

More information about the R-devel mailing list