Skip to content
Prev 82394 / 398502 Next

Change labels of x-axes in Plot of stl() function?

I noticed that we can combine the function and proto
approaches by placing the proto in the function with
these advantages:

1. the function body can be reduced to just two statements
2. no explicit manipulation of environments via
   environment(...) is required (as proto does that itself
   automatically)

In our new solution, the function signature and the
redefinition of mtext are the same as in our prior function
solution but the remaining lines in the function solution
(that manipulate environments) are replaced with the single
'with' command as shown.

As before, placing stats:::plot.stl in the proto results in
(1) a copy of that function being placed in the proto object and
(2) the environment of that copy being set to the proto object itself

The parent of that proto object defaults to its lexical environment
so all we need to do in order to ensure that xlab and the new mtext
are accessible from the copy of plot.stl are to ensure that they
are in that parent environment -- they need not be in the proto
object itself.  They will be accessed via inheritance anyways.  Thus
the solution reduces to just:

library(proto)
plot.stl <- function(..., xlab = "time") {
	mtext <- function(text, ...)
		graphics::mtext(if (text == "time") xlab else text, ...)
	with( proto(plot.stl = stats:::plot.stl), plot.stl(...) )
}

# test
example(stl)  # defines stdm for use in the test
plot.stl(stdm, xlab = "My xlab")
On 12/7/05, Gabor Grothendieck <ggrothendieck at gmail.com> wrote: