简体   繁体   中英

Error in plot.BsProb in R when trying to change xlab

I am using the package BsMD to plot marginal factor posterior probabilities for Bayesian screening (plot.BsProb). I wish to change the xlab and ylab, but I get an error:

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")

Error in plot.default(x, y[, 1], xlim = range(x), ylim = c(0, 1), type = "n", : formal argument "xlab" matched by multiple actual arguments.

May anyone please help me change the xlab and ylab for plotting "Models"?

You could temporarily change the par settings for label colors and add the labels afterwards using 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") 

在此处输入图片说明

Good old traceback() tells us what's happening here:

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")

Your Models object is S3-classed as BsProb :

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

Thus running plot(Models,...) dispatches to plot.BsProb() which exists under the BsMD private environment:

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>

Looking into the above code, we can see that it dynamically defines a spikes() function and calls it near the end of the body. The spikes() function calls plot() and unconditionally passes an argument of xlab='factors' , along with the ... variadic arguments relayed from the top-level call. That's why there are two xlab actual arguments matching against the xlab formal argument; one comes from the spikes() lexical argument list, and one comes from the ... relay.

Thus, unfortunately, there's no way to pass xlab from the top-level call without clashing with the implementation. You're out of luck. You can write a strongly-worded letter to the maintainer of the BsMD package requesting they make the BsMD:::plot.BsProb() function more flexible, and in the meantime just bite the bullet and use a hack to work around the limitation, like @fdetsch's excellent suggestion.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM