修复plot(fevd())函数的布局/绘图选项

时间:2015-11-10 14:32:16

标签: r variables plot

我目前正在使用vars包在R中进行Vector Autoregressive Analysis。我想知道以下事情是否可能:

1)以前的解决方案更好地适应页面工作正常,但现在有了更多的变量,我们有更多的情节,布局再次搞砸了。我已经玩了一些win.graph()参数,但没有什么能给我一个正确的可读解决方案。

2)vars包的绘图(irf(...)函数给出了每页输出的单图。我想知道这是否也可以用于绘图(fevd()函数添加一些额外的(未知)对我来说)参数选项

3)另外,为了便于阅读,我想给图形着色,绘图(fevd()给出了各种灰度输出,是否可以改变这些颜色?

my current output

提前谢谢

奥利弗

1 个答案:

答案 0 :(得分:1)

您必须修改fevd的绘图功能才能执行您想要的操作。这是一个修改后的plot.varfevd函数,用于删除对par()的所有调用。这样可以正确使用layout。已删除的行已被注释掉(#)。我还删除了要求确认的参数" single"图。

plot.varfevd  <-function (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, ...)
{
    K <- length(x)
    ynames <- names(x)
    plot.type <- match.arg(plot.type)
    if (is.null(names)) {
        names <- ynames
    }
    else {
        names <- as.character(names)
        if (!(all(names %in% ynames))) {
            warning("\nInvalid variable name(s) supplied, using first variable.\n")
            names <- ynames[1]
        }
    }
    nv <- length(names)
#    op <- par(no.readonly = TRUE)
    ifelse(is.null(main), main <- paste("FEVD for", names), main <- rep(main,
        nv)[1:nv])
    ifelse(is.null(col), col <- gray.colors(K), col <- rep(col,
        K)[1:K])
    ifelse(is.null(ylab), ylab <- rep("Percentage", nv), ylab <- rep(ylab,
        nv)[1:nv])
    ifelse(is.null(xlab), xlab <- rep("Horizon", nv), xlab <- rep(xlab,
        nv)[1:nv])
    ifelse(is.null(ylim), ylim <- c(0, 1), ylim <- ylim)
    ifelse(is.null(legend), legend <- ynames, legend <- legend)
    if (is.null(names.arg))
        names.arg <- c(paste(1:nrow(x[[1]])), rep(NA, addbars))
    plotfevd <- function(x, main, col, ylab, xlab, names.arg,
        ylim, ...) {
        addbars <- as.integer(addbars)
        if (addbars > 0) {
            hmat <- matrix(0, nrow = K, ncol = addbars)
            xvalue <- cbind(t(x), hmat)
            barplot(xvalue, main = main, col = col, ylab = ylab,
                xlab = xlab, names.arg = names.arg, ylim = ylim,
                legend.text = legend, ...)
            abline(h = 0)
        }
        else {
            xvalue <- t(x)
            barplot(xvalue, main = main, col = col, ylab = ylab,
                xlab = xlab, names.arg = names.arg, ylim = ylim,
                ...)
            abline(h = 0)
        }
    }
    if (plot.type == "single") {
#        par(mar = mar, oma = oma)
#        if (nv > 1)
#            par(ask = TRUE)
        for (i in 1:nv) {
            plotfevd(x = x[[names[i]]], main = main[i], col = col,
                ylab = ylab[i], xlab = xlab[i], names.arg = names.arg,
                ylim = ylim, ...)
        }
    }
    else if (plot.type == "multiple") {
        if (missing(nc)) {
            nc <- ifelse(nv > 4, 2, 1)
        }
        nr <- ceiling(nv/nc)
        par(mfcol = c(nr, nc), mar = mar, oma = oma)
        for (i in 1:nv) {
            plotfevd(x = x[[names[i]]], main = main[i], col = col,
                ylab = ylab[i], xlab = xlab[i], names.arg = names.arg,
                ylim = ylim, ...)
        }
    }
#    on.exit(par(op))
}

然后,您将需要短变量名称。如果需要,请选择首字母缩略词。

library(vars)
data(Canada)
colnames(Canada) <-c("name1","name2","name3","name4")
var <- VAR(Canada , p=4 , type = "both")

使用宽图窗口(使用win.graph)并使用layout(以获得八个图的位置),可以正确显示所有图表。我还根据要求改变了地块的颜色。最后,我们现在使用single图,因为没有par()的调用与layout()不匹配。

win.graph(width=15,height=8)
layout(matrix(1:8,ncol=2))
plot.varfevd(fevd(var, n.ahead = 10 ),plot.type = "single", col=1:4)
plot.varfevd(fevd(var, n.ahead = 10 ),plot.type = "single", col=1:4)

enter image description here