r - 在 R 中调整 plotmo::plot_glmnet 的顶轴标题和标签图

标签 r plot glmnet

我使用 r 包 plotmo 可视化 LASSO 回归的系数收缩。默认情况下,它会添加一个标题为“自由度”的顶轴。如何删除顶部标题或更改其内容?通常,如何调整 plotmo::plot_glmnet 绘制的顶轴(包括标题和轴标签)?

library(glmnet)
library(plotmo) 
fit = glmnet(as.matrix(mtcars[-1]), mtcars[,1])
plot_glmnet(fit,xvar='lambda',label=7)

enter image description here

我试过使用 mtextaxis 函数,但没有用:

plot_glmnet(fit,xvar='lambda',label=7)
mtext('new top title', side=3)

enter image description here

最佳答案

plot_glmnet 的代码中有一行,mtext(toplabel...) 就是这样做的。 不幸的是,如果你想删除它,你必须创建一个删除这一行的新函数,并分配命名空间:

new_plot_glmnet = function (x = stop("no 'x' argument"), xvar = c("rlambda", "lambda", 
    "norm", "dev"), label = 10, nresponse = NA, grid.col = NA, 
    s = NA, ...) 
{
    check.classname(x, "x", c("glmnet", "multnet"))
    obj <- x
    beta <- get.beta(obj$beta, nresponse)
    ibeta <- nonzeroCoef(beta)
    if (length(ibeta) == 0) {
        plot(0:1, 0:1, col = 0)
        legend("topleft", legend = "all glmnet coefficients are zero", 
            bty = "n")
        return(invisible(NULL))
    }
    beta <- as.matrix(beta[ibeta, , drop = FALSE])
    xlim <- dota("xlim", ...)
    xvar <- match.arg1(xvar)
    switch(xvar, norm = {
        if (inherits(obj, "multnet") || inherits(obj, "mrelnet")) {
            stop0("xvar=\"norm\" is not supported by plot_gbm for ", 
                "multiple responses (use plot.glmnet instead)")
        }
        x <- apply(abs(beta), 2, sum)
        if (!is.specified(xlim)) xlim <- c(min(x), max(x))
        xlab <- "L1 Norm"
        approx.f <- 1
    }, lambda = {
        x <- log(obj$lambda)
        if (!is.specified(xlim)) xlim <- c(min(x), max(x))
        xlab <- "Log Lambda"
        approx.f <- 0
    }, rlambda = {
        x <- log(obj$lambda)
        if (!is.specified(xlim)) xlim <- c(max(x), min(x))
        xlab <- "Log Lambda"
        approx.f <- 0
    }, dev = {
        x <- obj$dev.ratio
        if (!is.specified(xlim)) xlim <- c(min(x), max(x))
        xlab <- "Fraction Deviance Explained"
        approx.f <- 1
    })
    xlim <- fix.lim(xlim)
    if (xvar != "rlambda") 
        stopifnot(xlim[1] < xlim[2])
    else if (xlim[2] >= xlim[1]) 
        stop0("xlim[1] must be bigger than xlim[2] for xvar=\"rlambda\"")
    iname <- get.iname(beta, ibeta, label)
    old.par <- par("mar", "mgp", "cex.axis", "cex.lab")
    on.exit(par(mar = old.par$mar, mgp = old.par$mgp, cex.axis = old.par$cex.axis, 
        cex.lab = old.par$cex.lab))
    mar4 <- old.par$mar[4]
    if (length(iname)) {
        cex.names <- min(1, max(0.5, 2.5/sqrt(length(iname))))
        mar4 <- max(old.par$mar[4] + 1, 0.75 * cex.names * par("cex") * 
            max(nchar(names(iname))))
    }
    main <- dota("main", ...)
    nlines.needed.for.main <- if (is.specified(main)) 
        nlines(main) + 0.5
    else 0
    par(mar = c(old.par$mar[1], old.par$mar[2], max(old.par$mar[3], 
        nlines.needed.for.main + 2.6), mar4))
    par(mgp = c(1.5, 0.4, 0))
    par(cex.axis = 0.8)
    ylab <- "Coefficients"
    if (is.list(obj$beta)) 
        ylab <- paste0(ylab, ": Response ", rownames(obj$dfmat)[nresponse])
    coef.col <- get.coef.col(..., beta = beta)
    keep <- which((coef.col != "NA") & (coef.col != "0"))
    iname <- iname[iname %in% keep]
    beta[-keep, ] <- NA
    call.plot(graphics::matplot, force.x = x, force.y = t(beta), 
        force.main = "", force.col = coef.col, def.xlim = xlim, 
        def.xlab = xlab, def.ylab = ylab, def.lty = 1, def.lwd = 1, 
        def.type = "l", ...)
    abline(h = 0, col = "gray", lty = 3)
    maybe.grid(x = x, beta = beta, grid.col = grid.col, coef.col = coef.col, 
        ...)
    if (xvar == "rlambda") {
        annotate.rlambda(lambda = obj$lambda, x = x, beta = beta, 
            s = s, grid.col = grid.col, coef.col = coef.col, 
            ...)
        toplab <- "Lambda"
    }
    else {
        top.axis(obj, x, nresponse, approx.f)
        toplab <- "Degrees of Freedom"
    }
    #mtext(toplab, side = 3, line = 1.5, cex = par("cex") * par("cex.lab"))
    if (is.specified(main)) 
        mtext(main, side = 3, line = 3, , cex = par("cex"))
    if (length(iname)) 
        right.labs(beta, iname, cex.names, coef.col)
    invisible(NULL)
}

environment(new_plot_glmnet) <- asNamespace('plotmo')

然后你绘制:

new_plot_glmnet(fit,xvar='lambda',label=7)
mtext('new top title', side=3,padj=-2)

enter image description here

关于r - 在 R 中调整 plotmo::plot_glmnet 的顶轴标题和标签图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60236791/

相关文章:

R ggplot注释对齐

r - 使用插入符包和 R 绘制学习曲线

python - 使用稀疏数据在Python中绘制二维等值线图

r - 为什么需要使用 caret::train(..., method = "glmnet") 和 cv.glmnet() 调整 lambda?

r - 如何将函数应用于 data.frame 的每个元素?

Python numpy 等效于 R rep 和 rep_len 函数

r - 在 R 中对多行进行分组过滤

r - R中的惩罚 Gamma 回归

r - 如何从 cv.glmnet 中使用成本函数提取实际分类错误率,以便与 cv.glm 进行比较?

r - ggplot2 geom_violin 的非常奇怪的图表