Skip to content

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

6 messages · Gabor Grothendieck, Jan Verbesselt

#
If you look through the output of:

stats:::plot.stl

you see right near the end that "time" is hard coded in the call to mtext.

However, we could temporarily redefine mtext so that it works as you
wish and then redefine plot.stl so that it looks within the environment
of our function to find mtext (rather than looking in the stats package):

plot.stl <- function(..., xlab = "time") {
	mtext <- function(text, ...) graphics::mtext(xlab, ...)
	plot.stl <- stats:::plot.stl
	environment(plot.stl) <- environment()
	plot.stl(...)
}

# test it
example(stl)
plot.stl(stmd, xlab = "X")
On 12/7/05, Jan Verbesselt <Jan.Verbesselt at biw.kuleuven.be> wrote:
#
Thanks a lot!

However, it?s not working perfectly yet. The function plot.stl now also
changes the labels of Data, Seasonal, Trend, and Remainder to the text
defined for ?xlab?. How could this be fine-tuned?

Regards,
Jan
Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
#
One other thought.  This can be arguably done more compactly
using the proto package.  We define a proto object consisting
of three components:

- plot.stl which is just a copy of the corresponding routine
in the stats package,
- our redefined mtext and
- our desired x label.

and then run plot.stl in the scope of the proto object.  There
are two simplifications here:

1. we don't need to do explicit manipulations of environments
since proto automatically resets the environments of component
functions.  In particular, the environment of plot.stl is automatically
reset to point to the proto object so that its scope is no longer
the stats package.

2. we don't need to redefine the argument list of plot.stl since
with its environment automatically redefined as just explained,
plot.stl will look for xlab in the proto object so we can just put it
there rather than pass it in a new, different, argument list

library(proto)
STL <- proto(plot.stl = stats:::plot.stl,
	mtext = function(text, ...) graphics::mtext(xlab, ...),
	xlab = "time (decade)")

# test.  First line computes stmd to be used as test data.
example(stl)
with(STL, plot.stl(stmd))



# note that to change the xlab we can do this

STL$xlab <- "New xlab"
with(STL, plot.stl(stmd))
On 12/7/05, Gabor Grothendieck <ggrothendieck at gmail.com> wrote:
#
This should fix that problem:

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


Also for the proto solution try this:

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

# test.  First line computers stmd to be used as test data.
example(stl)
with(STL, plot.stl(stmd))
On 12/7/05, Jan Verbesselt <Jan.Verbesselt at biw.kuleuven.be> wrote:
#
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: