1"plot.varfevd" <-
2function (x, plot.type = c("multiple", "single"), names = NULL, main = NULL, col = NULL, ylim = NULL, ylab = NULL, xlab = NULL, legend = NULL, names.arg = NULL, nc, mar = par("mar"), oma = par("oma"), addbars = 1, ...)
3{
4  K <- length(x)
5  ynames <- names(x)
6  plot.type <- match.arg(plot.type)
7  if (is.null(names)) {
8    names <- ynames
9  } else {
10    names <- as.character(names)
11    if (!(all(names %in% ynames))) {
12      warning("\nInvalid variable name(s) supplied, using first variable.\n")
13      names <- ynames[1]
14    }
15  }
16  nv <- length(names)
17  op <- par(no.readonly = TRUE)
18  ifelse(is.null(main), main <- paste("FEVD for", names), main <- rep(main, nv)[1:nv])
19  ifelse(is.null(col), col <- gray.colors(K), col <- rep(col, K)[1:K])
20  ifelse(is.null(ylab), ylab <- rep("Percentage", nv), ylab <- rep(ylab, nv)[1:nv])
21  ifelse(is.null(xlab), xlab <- rep("Horizon", nv), xlab <- rep(xlab, nv)[1:nv])
22  ifelse(is.null(ylim), ylim <- c(0, 1), ylim <- ylim)
23  ifelse(is.null(legend), legend <- ynames, legend <- legend)
24  if(is.null(names.arg)) names.arg <- c(paste(1:nrow(x[[1]])), rep(NA, addbars))
25  plotfevd <- function(x, main, col, ylab, xlab, names.arg, ylim, ...){
26    addbars <- as.integer(addbars)
27    if(addbars > 0){
28      hmat <- matrix(0, nrow = K, ncol = addbars)
29      xvalue <- cbind(t(x), hmat)
30      barplot(xvalue, main = main, col = col, ylab = ylab, xlab = xlab, names.arg = names.arg, ylim = ylim, legend.text = legend, ...)
31      abline(h = 0)
32    } else {
33      xvalue <- t(x)
34      barplot(xvalue, main = main, col = col, ylab = ylab, xlab = xlab, names.arg = names.arg, ylim = ylim, ...)
35      abline(h = 0)
36    }
37  }
38  if (plot.type == "single") {
39    par(mar = mar, oma = oma)
40    if (nv > 1) par(ask = TRUE)
41    for (i in 1:nv) {
42      plotfevd(x = x[[names[i]]], main = main[i], col = col, ylab = ylab[i], xlab = xlab[i], names.arg = names.arg, ylim = ylim, ...)
43    }
44  } else if (plot.type == "multiple") {
45    if (missing(nc)) {
46      nc <- ifelse(nv > 4, 2, 1)
47    }
48    nr <- ceiling(nv / nc)
49    par(mfcol = c(nr, nc), mar = mar, oma = oma)
50    for (i in 1:nv) {
51      plotfevd(x = x[[names[i]]], main = main[i], col = col, ylab = ylab[i], xlab = xlab[i], names.arg = names.arg, ylim = ylim, ...)
52    }
53  }
54  on.exit(par(op))
55}
56