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

Prof Brian Ripley ripley at stats.ox.ac.uk
Mon Apr 7 14:01:46 CEST 2008

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

> Dear list:
> I would like to override the default way R formats plot axes with a
> custom method(s). Obviously I would prefer to define it as general as
> possible avoiding writing a custom method for each individual class
> where possible.
> The plot.default method (and I assume other methods as well) calls
> Axis(...) to produce the axis layout depending on data. In this sense it
> seems reasonable to (re)define Axis methods rather than change plot
> methods (which are impossible to control as there can be too many custom
> ones).
> Now my question is how can I redefine Axis that it is automatically
> called by plot.default and other plot methods? Or which Axis-method
> signatures are already defined that I should redefine?

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.

methods("Axis") answers what I think you meant to ask.

> What I have succeeded so far was defining Axis.numeric and Axis.myClass,
> which are called by default if I supply data of class numeric or
> myClass. For example, a simple code like
> Axis.numeric = AxisFUN = function(x=NULL, at=NULL, ..., side,
> labels=TRUE) {
>    if (is.null(at)) at = pretty(x)
>    axis(at=at, ..., side=side, labels=labels, col="red", lwd=5)
> }
> run with plot(1:5,1:5) will format both axes red.
> However, if I execute it with plot(1:5) only x axis plotting is
> intercepted leaving y at default formatting although it is also numeric.
> Why and what should I define to intercept for plotting the y axis or for
> plotting axes in boxplot etc. Simply importing and overriding Axis as
> function does not bring anything, it wouldn't get called.

Try debug(Axis) and see what it is called with.  (I have altered that in 
current versions of R -- you didn't say what yours was.)

> Also I was not able to use S4 methods to redefine Axis. Overriding it
> with the code from above using any of the following signatures also
> didn't work for me - they are simply ignored:
> setGeneric("Axis")
> setMethod("Axis", signature(x="missing",at="numeric"), AxisFUN)
> setMethod("Axis", signature(x="numeric",at="missing"), AxisFUN)
> setMethod("Axis", signature(x="missing",at="ANY"), AxisFUN)
> setMethod("Axis", signature(x="ANY",at="missing"), AxisFUN)
> setMethod("Axis", signature(x="ANY",at="ANY"), AxisFUN)

Namespaces ... (perhaps you need to study them?).

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