multi.time.series <- function(x,
digits=2,
ylab="Set the 'ylab' parameter to a title for this axis!",
xlab="Set the 'xlab' parameter to a title for this axis!",
main="Set the 'main' parameter to a title for this plot!"
) {
par(xpd=NA,mar=c(5,0,1,7))
## Convert date strings to POSIXct seconds
x <- lapply(x,function(L){L$d <- as.POSIXct(strptime(L$d,"%F"));L})
# range of all values of sublists
attr.range <- function(at)
range(unlist(lapply(x,function(L)L[[at]])))
xlim <- attr.range('d')
ylim <- attr.range('y')
plot(xlim,ylim,type='n',bty='n',xaxt='n',yaxt='n',main=main,xlab='')
## prepare for plotting by sorting and associating a color
colrs <- rainbow(length(x))
S <- lapply(x,sorted)
x <- apply(rbind(S,colrs),2,function(L){L$S$col <- L$col;L$S})
sapply(x,mts.lines)
xlabsecs <- as.integer(unique(c(sapply(x,function(L)L$d),recursive=T)))
class(xlabsecs) <- "POSIXct"
mtext(xlab,1,line=4)
axis(1,xlabsecs,format(xlabsecs),las=2,lty=0,line=-2)
mtext(ylab,4,line=5)
ax.y <- unique(c(ylim,sapply(x,function(L)L$y[1])))
axis(4,round(ax.y,digits),lty=0,las=1,line=2)
}
sorted <- function(L){
index <- sort.int(L$d,i=T,dec=T)$ix
L$d <- as.numeric(L$d[index])
L$y <- L$y[index]
L
}
mts.lines <- function(L){
lines(L$d,L$y,col=L$col)
text(L$d[1],L$y[1],L$name,pos=4,col=L$col)
}
##debug(multi.time.series)