Re: [Rd] Overriding axis formatting with custom Axis method, Axis.numeric etc
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 space). 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: > LC_CTYPE=en_GB.UTF-8;LC_NUMERIC=C;LC_TIME=en_GB.UTF-8;LC_COLLATE=C;LC_MO > NETARY=en_GB.UTF-8;LC_MESSAGES=en_GB.UTF-8;LC_PAPER=en_GB.UTF-8;LC_NAME= > C;LC_ADDRESS=C;LC_TELEPHONE=C;LC_MEASUREMENT=en_GB.UTF-8;LC_IDENTIFICATI > 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, [EMAIL PROTECTED] 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, UKFax: +44 1865 272595 __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] Overriding axis formatting with custom Axis method, Axis.numeric etc
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'. 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? > 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. Regards, Oleg > sessionInfo() R version 2.6.2 (2008-02-08) x86_64-unknown-linux-gnu locale: LC_CTYPE=en_GB.UTF-8;LC_NUMERIC=C;LC_TIME=en_GB.UTF-8;LC_COLLATE=C;LC_MO NETARY=en_GB.UTF-8;LC_MESSAGES=en_GB.UTF-8;LC_PAPER=en_GB.UTF-8;LC_NAME= C;LC_ADDRESS=C;LC_TELEPHONE=C;LC_MEASUREMENT=en_GB.UTF-8;LC_IDENTIFICATI 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 ** The contents of this email are for the named addressee(s) only. It contains information which may be confidential and privileged. If you are not the intended recipient, please notify the sender immediately, destroy this email and any attachments and do not otherwise disclose or use them. Email transmission is not a secure method of communication and Man Investments cannot accept responsibility for the completeness or accuracy of this email or any attachments. Whilst Man Investments makes every effort to keep its network free from viruses, it does not accept responsibility for any computer virus which might be transferred by way of this email or any attachments. This email does not constitute a request, offer, recommendation or solicitation of any kind to buy, subscribe, sell or redeem any investment instruments or to perform other such transactions of any kind. Man Investments reserves the right to monitor, record and retain all electronic communications through its network to ensure the integrity of its systems, for record keeping and regulatory purposes. Visit us at: www.maninvestments.com __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] Overriding axis formatting with custom Axis method, Axis.numeric etc
Try this: Axis.AsIs = 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) } plot(I(1:10)) On Mon, Apr 7, 2008 at 7:22 AM, Sklyar, Oleg (MI London) <[EMAIL PROTECTED]> 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? > > 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. > > 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) > > Any ideas? > > Thanks, > Oleg > > Dr Oleg Sklyar > Technology Group > Man Investments Ltd > +44 (0)20 7144 3803 > [EMAIL PROTECTED] > > > ** > The contents of this email are for the named addressee(s) only. > It contains information which may be confidential and privileged. > If you are not the intended recipient, please notify the sender > immediately, destroy this email and any attachments and do not > otherwise disclose or use them. Email transmission is not a > secure method of communication and Man Investments cannot accept > responsibility for the completeness or accuracy of this email or > any attachments. Whilst Man Investments makes every effort to keep > its network free from viruses, it does not accept responsibility > for any computer virus which might be transferred by way of this > email or any attachments. This email does not constitute a request, > offer, recommendation or solicitation of any kind to buy, subscribe, > sell or redeem any investment instruments or to perform other such > transactions of any kind. Man Investments reserves the right to > monitor, record and retain all electronic communications through > its network to ensure the integrity of its systems, for record > keeping and regulatory purposes. > > Visit us at: www.maninvestments.com > > __ > R-devel@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel > __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] Overriding axis formatting with custom Axis method, Axis.numeric etc
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, [EMAIL PROTECTED] 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, UKFax: +44 1865 272595 __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] Overriding axis formatting with custom Axis method, Axis.numeric etc
I think that you can change the axis as a argument in plot: custom.axis <- bquote({axis(1, col="red", lwd=5) axis(2, col="red", lwd=5)}) suppressWarnings(plot(1:5, 1:5, frame.plot = eval(custom.axis))) suppressWarnings(plot(1:5, frame.plot = eval(custom.axis))) On Mon, Apr 7, 2008 at 8:22 AM, Sklyar, Oleg (MI London) < [EMAIL PROTECTED]> 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? > > 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. > > 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) > > Any ideas? > > Thanks, > Oleg > > Dr Oleg Sklyar > Technology Group > Man Investments Ltd > +44 (0)20 7144 3803 > [EMAIL PROTECTED] > > > ** > The contents of this email are for the named addressee(s) only. > It contains information which may be confidential and privileged. > If you are not the intended recipient, please notify the sender > immediately, destroy this email and any attachments and do not > otherwise disclose or use them. Email transmission is not a > secure method of communication and Man Investments cannot accept > responsibility for the completeness or accuracy of this email or > any attachments. Whilst Man Investments makes every effort to keep > its network free from viruses, it does not accept responsibility > for any computer virus which might be transferred by way of this > email or any attachments. This email does not constitute a request, > offer, recommendation or solicitation of any kind to buy, subscribe, > sell or redeem any investment instruments or to perform other such > transactions of any kind. Man Investments reserves the right to > monitor, record and retain all electronic communications through > its network to ensure the integrity of its systems, for record > keeping and regulatory purposes. > > Visit us at: www.maninvestments.com > > __ > R-devel@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel > -- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]] __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
[Rd] Overriding axis formatting with custom Axis method, Axis.numeric etc
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? 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. 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) Any ideas? Thanks, Oleg Dr Oleg Sklyar Technology Group Man Investments Ltd +44 (0)20 7144 3803 [EMAIL PROTECTED] ** The contents of this email are for the named addressee(s) only. It contains information which may be confidential and privileged. If you are not the intended recipient, please notify the sender immediately, destroy this email and any attachments and do not otherwise disclose or use them. Email transmission is not a secure method of communication and Man Investments cannot accept responsibility for the completeness or accuracy of this email or any attachments. Whilst Man Investments makes every effort to keep its network free from viruses, it does not accept responsibility for any computer virus which might be transferred by way of this email or any attachments. This email does not constitute a request, offer, recommendation or solicitation of any kind to buy, subscribe, sell or redeem any investment instruments or to perform other such transactions of any kind. Man Investments reserves the right to monitor, record and retain all electronic communications through its network to ensure the integrity of its systems, for record keeping and regulatory purposes. Visit us at: www.maninvestments.com __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel