首页 文章

尝试更改xlab时,R中plot.BsProb出错

提问于
浏览
0

我使用包BsMD绘制贝叶斯筛选的边际因子后验概率(plot.BsProb) . 我想改变xlab和ylab,但是我收到一个错误:

library(BsMD) 

X = matrix(c(1, 1, 1, 1, 1,-1,-1,-1,-1,-1,-1, 1, 1, 1,-1,-1,-1,-1,-1,
             1,-1, 1,-1,-1, 1,-1, 1,-1, 1,-1,-1,-1,-1,-1, 1,-1,-1,-1,
            -1,-1, 1, 1, 1, 1, 1,-1,-1,-1, 1,-1, 1, 1, 1, 1,-1,-1, 1,
             1, 1, 1,-1,-1, 1, 1, 1,-1,-1,-1, 1,-1,-1, 1, 1, 1,-1,-1,
             1, 1,-1, 1,-1, 1,-1,-1, 1,-1, 1, 1, 1,-1, 1,-1, 1, 1,-1
             ),nrow=19,ncol=5)    
y = matrix(c(6,2,4,11,17,4,2,6,18,11,10,12,12,15,5,9,11,10,9),nrow=19,ncol=1)

Models = BsProb(X = X, y = y, blk = 0, mFac = 5, mInt = 1, p = 0.25, g = 2.5, ng = 1, nMod = 5)
plot(Models, code=FALSE, xlab="asdasdasdasd")

plot.default中的错误(x,y [,1],xlim = range(x),ylim = c(0,1),type =“n”,:形式参数“xlab”由多个实际参数匹配 .

愿有人可以帮我改变xlab和ylab来绘制“模型”吗?

2 回答

  • 1

    您可以暂时更改标签颜色的 par 设置,然后使用 title 添加标签 .

    ## create plot with white axis labels
    par(col.lab = "white")
    plot(Models, code = FALSE)
    
    ## add customized axis labels
    par(col.lab = "black")
    title(xlab = "X-axis label", ylab = "Y-axis label")
    

    enter image description here

  • 1

    好老traceback()告诉我们这里发生了什么:

    plot(Models, code=FALSE, xlab="asdasdasdasd");
    ## Error in plot.default(x, y[, 1], xlim = range(x), ylim = c(0, 1), type = "n",  :
    ##   formal argument "xlab" matched by multiple actual arguments
    traceback();
    ## 5: plot.default(x, y[, 1], xlim = range(x), ylim = c(0, 1), type = "n",
    ##        xlab = "factors", ylab = "posterior probability", frame = FALSE,
    ##        axes = FALSE, ...)
    ## 4: plot(x, y[, 1], xlim = range(x), ylim = c(0, 1), type = "n",
    ##        xlab = "factors", ylab = "posterior probability", frame = FALSE,
    ##        axes = FALSE, ...)
    ## 3: spikes(prob, ...)
    ## 2: plot.BsProb(Models, code = FALSE, xlab = "asdasdasdasd")
    ## 1: plot(Models, code = FALSE, xlab = "asdasdasdasd")
    

    您的 Models 对象被S3分类为 BsProb

    class(Models);
    ## [1] "BsProb" "list"
    

    因此,运行 plot(Models,...) 调度到 plot.BsProb() ,它存在于 BsMD 私有环境下:

    BsMD:::plot.BsProb;
    ## function (x, code = TRUE, prt = FALSE, cex.axis = par("cex.axis"),
    ##     ...)
    ## {
    ##     spikes <- function(prob, lwd = 3, ...) {
    ##         y <- prob
    ##         n <- nrow(y)
    ##         x <- seq(n)
    ##         lab <- rownames(prob)
    ##         plot(x, y[, 1], xlim = range(x), ylim = c(0, 1), type = "n",
    ##             xlab = "factors", ylab = "posterior probability",
    ##             frame = FALSE, axes = FALSE, ...)
    ##         if (ncol(y) == 1) {
    ##             for (i in x) segments(x[i], 0, x[i], y[i, 1], lwd = lwd,
    ##                 col = grey(0.2))
    ##         }
    ##         else {
    ##             y[, 1] <- apply(prob, 1, min)
    ##             y[, 2] <- apply(prob, 1, max)
    ##             for (i in x) {
    ##                 segments(x[i], 0, x[i], y[i, 2], lwd = lwd, col = grey(0.8),
    ##                   lty = 1)
    ##                 segments(x[i], 0, x[i], y[i, 1], lwd = lwd, col = grey(0.2),
    ##                   lty = 1)
    ##             }
    ##         }
    ##         axis(1, at = x, labels = lab, line = 0, cex.axis = cex.axis)
    ##         axis(2, cex.axis = cex.axis)
    ##         invisible(NULL)
    ##     }
    ##     if (!any(class(x) == "BsProb"))
    ##         return("\nArgument `x' should be class BsProb. Output of corresponding function.")
    ##     ifelse(x$INDGAM == 0, prob <- as.matrix(x$sprob), prob <- x$prob)
    ##     if (code)
    ##         rownames(prob) <- rownames(x$prob)
    ##     else rownames(prob) <- names(x$sprob)
    ##     spikes(prob, ...)
    ##     if (prt)
    ##         summary.BsProb(x)
    ##     invisible(NULL)
    ## }
    ## <environment: namespace:BsMD>
    

    查看上面的代码,我们可以看到它动态定义了一个 spikes() 函数,并在正文的末尾调用它 . spikes() 函数调用 plot() 并无条件地传递 xlab='factors' 的参数,以及从顶级调用中继的 ... 可变参数 . 这就是为什么有两个 xlab 实际参数匹配 xlab 形式参数;一个来自 spikes() 词法参数列表,一个来自 ... 中继 .

    因此,遗憾的是,没有办法从顶层调用传递 xlab 而不与实现发生冲突 . 你运气不好您可以向 BsMD 包的维护者写一封措辞强硬的信,要求他们使 BsMD:::plot.BsProb() 函数更灵活,同时只需咬紧牙关并使用黑客来解决限制问题,比如@ fdetsch的优秀建议 .

相关问题