| 1 |
p.ts <- |
| 2 |
function(x, nrplots = max(1, min(8, n%/%400)), overlap = nk %/% 16, |
| 3 |
date.x = NULL, do.x.axis = !is.null(date.x), do.x.rug = FALSE, |
| 4 |
ax.format, main.tit = NULL, ylim = NULL, ylab = "", xlab = "Time", |
| 5 |
quiet = FALSE, mgp = c(1.25, .5, 0), ...) |
| 6 |
{ |
| 7 |
## Purpose: plot.ts with multi-plots + Auto-Title -- currently all on 1 page |
| 8 |
## ------------------------------------------------------------------------- |
| 9 |
## Arguments: x : timeseries [ts,rts,its,cts] or numeric vector |
| 10 |
## nrplots: number of sub-plots [DEFAULT: in {1..8}, ~= n/400] |
| 11 |
## overlap: how much should subsequent plots overlap [DEFAULT:..] |
| 12 |
## |
| 13 |
## Depends on mult.fig() |
| 14 |
## |
| 15 |
## ---> help page ?p.ts |
| 16 |
## |
| 17 |
## ------------------------------------------------------------------------- |
| 18 |
## Author: Martin Maechler, Date: 1 Jul 1994; 18 Dec 1998. |
| 19 |
|
| 20 |
if(is.null(main.tit)) main.tit <- paste(deparse(substitute(x))) |
| 21 |
isMat <- is.matrix(x) |
| 22 |
n <- if(isMat) nrow(x) else length(x) |
| 23 |
has.date.x <- !is.null(date.x) |
| 24 |
if(do.x.axis && !has.date.x) |
| 25 |
stop("'do.x.axis' is true, but 'date.x' is NULL") |
| 26 |
if(has.date.x) { |
| 27 |
if(n != length(date.x)) |
| 28 |
stop("'date.x' must be date vector of the same length as series") |
| 29 |
if(do.x.axis) |
| 30 |
date.x <- as.POSIXct(date.x) # work, or give error now |
| 31 |
if(is.unsorted(date.x, na.rm=TRUE)) { |
| 32 |
i <- order(date.x) |
| 33 |
x <- if(isMat) x[i,] else x[i] |
| 34 |
date.x <- date.x[i] |
| 35 |
} |
| 36 |
xaxt <- "n" |
| 37 |
} else xaxt <- par("xaxt") |
| 38 |
if(nrplots == 1) { |
| 39 |
if(has.date.x) { |
| 40 |
plot(date.x, x, ..., ylim = ylim, type = 'l', |
| 41 |
main = main.tit, xlab = xlab, ylab = ylab, xaxt = xaxt) |
| 42 |
if(do.x.axis) axis.POSIXct(1, x = date.x, format = ax.format) |
| 43 |
} |
| 44 |
else |
| 45 |
plot.ts(x, ..., ylim = ylim, |
| 46 |
main = main.tit, xlab = xlab, ylab = ylab, xaxt = xaxt) |
| 47 |
} |
| 48 |
else if(nrplots <= 0) |
| 49 |
return(nrplots) |
| 50 |
else { # nrplots >= 2 : |
| 51 |
if(n <= 1) stop("`x' must have at least two points!") |
| 52 |
if(!is.ts(x)) x <- as.ts(x) |
| 53 |
##- do.dates <- inherits(x, "cts") |
| 54 |
##- if(do.dates) x <- as.rts(x)# dates() as below fails [S+ 3.4] |
| 55 |
## NB: end() and start() are of length 1 _or_ 2 (!) |
| 56 |
scal <- (end(x) - (t1 <- start(x))) / (n-1) |
| 57 |
nk <- n %/% nrplots |
| 58 |
if(is.null(ylim)) |
| 59 |
ylim <- range(pretty(range(x, na.rm = TRUE))) |
| 60 |
if(!quiet) |
| 61 |
Form <- function(x) |
| 62 |
paste0("(",paste(formatC(x, digits=6, width=1), collapse=", "), ")") |
| 63 |
pp <- mult.fig(mfrow=c(nrplots,1), main = main.tit, |
| 64 |
mgp = mgp, marP = c(-1,-1,-2,0)) |
| 65 |
on.exit(par(pp $ old.par)) |
| 66 |
for(i in 1:nrplots) { |
| 67 |
i0 <- as.integer(max(0, (-overlap + (i-1)*nk)-1) ) |
| 68 |
in1 <- as.integer(min(n, i*nk + overlap)-1 ) |
| 69 |
st <- t1 + scal*i0 ##; if(do.dates) st <- dates(st) |
| 70 |
en <- t1 + scal*in1 ##; if(do.dates) en <- dates(en) |
| 71 |
if(!quiet) |
| 72 |
cat(sprintf("%2d -- start{%d}= %s; end{%d}= %s\n", |
| 73 |
i, i0,Form(st), in1, Form(en))) |
| 74 |
if(has.date.x) { |
| 75 |
plot(date.x[1+ i0:in1], window(x, start= st, end = en), |
| 76 |
..., ylim = ylim, type = 'l', |
| 77 |
xlab = xlab, ylab = ylab, xaxt = xaxt) |
| 78 |
if(do.x.axis) { |
| 79 |
if(!quiet) { |
| 80 |
cat("summary(date.x):\n"); print(summary(date.x[1+ i0:in1])) |
| 81 |
} |
| 82 |
axis.POSIXct(1, x = date.x[1+ i0:in1], format = ax.format) |
| 83 |
## (I've lost my improved version of this which had 'nYrs = 12' |
| 84 |
|
| 85 |
if(do.x.rug) ## this can be ugly |
| 86 |
rug(date.x[1+ i0:in1]) |
| 87 |
} |
| 88 |
} |
| 89 |
else |
| 90 |
plot(window(x, start= st, end = en), ylim = ylim, |
| 91 |
xlab = xlab, ylab = ylab, xaxt = xaxt, ..., |
| 92 |
plot.type= "single")# plot.type : for plot.mts only, |
| 93 |
} |
| 94 |
} |
| 95 |
} |