[R] ts; decompose; plot and title
Gavin Simpson
gavin.simpson at ucl.ac.uk
Tue Feb 27 18:53:17 CET 2007
On Tue, 2007-02-27 at 15:24 -0200, Alberto Monteiro wrote:
> Is there any way to give a "decent" title after I plot something
> generated by decompose?
>
> For example:
>
> # generate something with period 12
> x <- rnorm(600) + sin(2 * pi * (1:600) / 12)
>
> # transform to a monthy time series
> y <- ts(x, frequency=12, start=c(1950,1))
>
> # decompose
> z <- decompose(y)
>
> # plot
> plot(z)
>
> Now, the title is the ugly "Decomposition of additive time series".
> How can do this with a decent title, like "Analysis of UFO abductions"?
>
> Alberto Monteiro
It is because plot.decompose.ts decides to impose it's own title for
some reason (using getAnywhere(plot.decompose.ts) to get the function
definition):
function (x, ...)
{
plot(cbind(observed = x$random + if (x$type == "additive")
x$trend + x$seasonal
else x$trend * x$seasonal, trend = x$trend, seasonal = x$seasonal,
random = x$random), main = paste("Decomposition of",
x$type, "time series"), ...)
}
I'd just write your own wrapper instead, using plot.decompose.ts, along
the lines of:
decomp.plot <- function(x, main = NULL, ...)
{
if(is.null(main))
main <- paste("Decomposition of", x$type, "time series")
plot(cbind(observed = x$random + if (x$type == "additive")
x$trend + x$seasonal
else x$trend * x$seasonal, trend = x$trend, seasonal = x$seasonal,
random = x$random), main = main, ...)
}
#then to complete your example:
# generate something with period 12
x <- rnorm(600) + sin(2 * pi * (1:600) / 12)
# transform to a monthy time series
y <- ts(x, frequency=12, start=c(1950,1))
# decompose
z <- decompose(y)
# plot
decomp.plot(z, main = "Analysis of UFO abductions")
Perhaps you could also file a bug report under the wish list category,
showing your example and the fact that
plot(z, main = "Analysis of UFO abductions")
gives this error:
Error in plotts(x = x, y = y, plot.type = plot.type, xy.labels =
xy.labels, :
formal argument "main" matched by multiple actual arguments
It isn't really a bug, but an infelicity in the way the function
currently works - my decomp.plot function may even be a suitable patch
or maybe the following is better:
decomp.plot2 <- function(x, main, ...)
{
if(missing(main))
main <- paste("Decomposition of", x$type, "time series")
plot(cbind(observed = x$random + if (x$type == "additive")
x$trend + x$seasonal
else x$trend * x$seasonal, trend = x$trend, seasonal = x$seasonal,
random = x$random), main = main, ...)
}
HTH
G
--
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
Gavin Simpson [t] +44 (0)20 7679 0522
ECRC, UCL Geography, [f] +44 (0)20 7679 0565
Pearson Building, [e] gavin.simpsonATNOSPAMucl.ac.uk
Gower Street, London [w] http://www.ucl.ac.uk/~ucfagls/
UK. WC1E 6BT. [w] http://www.freshwaters.org.uk
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
More information about the R-help
mailing list