plotir <- function(resp, var=NULL, shock=NULL, type=c("multiple","single"),main="Impulse response plot", yax.flip=TRUE, vfirst=TRUE, file="irplot.pdf") { ## resp: impulse responses, as produced by impulsdtrf ## If 4-dimensional, multiple lines per plot (e.g. error bands) ## default is colors symmetric around center black ## var: indexes of variables whose responses are to be plotted ## shock: indexes of shocks whose effects are to be plotted ## yax.flip: Unless very few responses are plotted, vertical axis ## labels start to overlap, unless yax.flip=TRUE ## vfirst: the variable index increases down each column. Otherwise ## (vfirst=FALSE) the shock index does so. ## As opposed to more direct plotting commands, this one has the ## advantage that (1) it scales all responses of the same variable ## to the same height on a given graph, (2) that it allows printing ## a matrix of graphs with rows corresponding to variables and columns ## to shocks, or vice versa, and (3) that it prompts for the legend ## (in the "single" case). ##------------------------------- nv <- dim(resp)[1] ns <- dim(resp)[2] if (length(dim(resp)) == 4) { nlines <- dim(resp)[4] } else { nlines <- 1 resp <- array(resp, c(dim(resp), 1), dimnames=c(dimnames(resp), NULL)) } if ( is.null(var) ) var <- 1:nv if (is.null(shock)) shock <- 1:ns pr <- resp[var,shock, , ,drop=FALSE] dimnames(pr) <- list(var=dimnames(resp)[[1]][var], shock=dimnames(resp)[[2]][shock],NULL, NULL) ## pdf(file=file, width=21, height=24) x11(width=21, height=24) ybound <- matrix(0,2,length(var)) for (iv in 1:length(var)) ybound[ , iv] <- range(pr[iv, , , ]) if(!vfirst) pr <- aperm(pr, c(2,1,3,4)) nr <- dim(pr)[1] nc <- dim(pr)[2] np <- dim(pr)[3] par(omi=c(1,1,2,1)) op <- par(mfrow=c(nr,nc)) center <- dim(resp)[4] %/% 2 + 1 # assumes odd number of plot lines for (ir in 1:nr) { for (ic in 1:nc) { if (ic == 1) { par(mai=c(1/nr,6/nc,2/nr,1/nc)) } else { par(mai=c(1/nr,2/nr,2/nr,1/nc)) } plot(0:(np-1), pr[ir, ic, , center],bty="l", type="l", ylim=ybound[ , if(vfirst) ir else ic], frame=TRUE, ylab=if(ic==1) dimnames(pr)[[1]][ir] else "", xlab="", main=if(ir ==1) dimnames(pr)[[2]][ic] else "", xaxt=if(ir==nr)"s" else "n", yaxt=if(ic==1)"s" else "n", cex.main=2, cex.lab=2) lines(c(0,(np-1)),c(0,0)) #x axis line if ( nlines > 1 ) { for (il in (1:(nlines %/% 2))) { lines(0:(np-1), pr[ir,ic, , center + il], col=il+1) lines(0:(np-1), pr[ir,ic, , center - il], col=il+1) } } grid() } } title(main, outer=TRUE, cex.main=4) ## dev.off() dev.copy2pdf(file=file) dev.flush() par(op) }