R - 生成绘图而不显示它

R - Generate a plot without displaying it

我编写了一个函数,它使用循环 在网格 中显示数据集中变量的直方图。我正在为条形图添加标签,但最高条形图的标签总是被情节截断一半。 我一直在寻找一种方法来扩展绘图边距,但在任何地方都找不到。 (par(oma) 或 par(mar)) 对我不起作用。我想到了一个解决方法,它首先在一个函数中生成一个直方图,从中获取 ylim 和 return 那些限制,然后原始循环可以使用它来在 hist() 函数中定义扩展的 ylim。

但是 return 的函数 ylim() 也绘制直方图。我想知道是否有办法不显示情节。我尝试了 dev.off() 但这也关闭了循环中的情节。

函数returns ylim

histylim = function(data, nclass=NULL) {
    a = hist(unlist(data), nclass=nclass)
    corners = par("usr")
#    dev.off()
    return(c(corners[3], corners[4]))
}

除了指定的 return() 对象之外,上述函数还 return 是一个直方图。
添加 def.off() 并在另一个生成直方图网格的循环中调用该函数时,histylim 中的 dev.off() 也会关闭网格。

这是我写的函数 return 一串直方图

multihist = function(data, numcols, nclass=NULL, labels=NULL, perc=F, axis=F, quants=NULL, 
                     plot_dim=NULL) {

    vars = names(which(sapply(data, is.numeric)))
    numrows = if (length(vars)%%numcols == 0) ((length(vars)%%numcols)+1) else ((length(vars)%/%numcols)+1)

    if (is.null(plot_dim)) {options(repr.plot.width=17, repr.plot.height=5*numrows)} 
        else {options(repr.plot.width=plot_dim[1], repr.plot.height=plot_dim[2])}
    
    histylim = function(data, nclass=NULL) {
        a = hist(unlist(data), nclass=nclass)
        corners = par("usr")
#         dev.off()
        return(c(corners[3], corners[4]))
    }
        
    par(mfrow=c(numrows, numcols))
    par(mar=c(5.1, 4.1, 4.1+2, 2.1)) 
    par(oma=c(0,0+2,0,0))
    
    for (i in seq(length(vars))) {
        var = vars[i] 
        par(mai=c(0, 0, 0, 0))
        corners = par('usr')
        a = hist(unlist(data[, var]), nclass=nclass, col=c(i+1), 
                 ylim=histylim(unlist(data[, var]), nclass=nclass),
                 main=paste(var), xlab=vars[i], ylab='freq', cex.main=2, cex.lab=1.7, cex.axis=1.5)
        median.var = median(unlist(data[,var]))
        mtext(paste('median : ', median.var), 3, adj = 0.5, line = 0.5, cex=1.2)
        abline(v=median.var, lty=2, lwd=2)
        if (!is.null(quants)) {for (q in quantile(unlist(df[,var]), quants)) {
            abline(v=q,lty=2,lwd=1,col='grey')}}
        if (axis==T) {axis(side=1, at=a$breaks, label=a$breaks)}
        if (!is.null(labels)) {
            if (perc==T) {
                labs = paste0(round(a$counts/sum(a$counts)*100),'%')
                labs[which(a$counts == 0)] = ''
                text(a$mids, a$counts, labels=labs, cex=labels, pos=3, col=1, font.axis=1)}
            else {text(a$mids, a$counts, labels=a$counts, cex=labels, pos=3, col=1, font.axis=1)}
        }
    }
}

我希望最高的直方图标签不被切成两半。

编辑:

简单地添加 plot = FALSE 在这里不起作用,因为您想获得一些在绘图时产生的参数。在这种情况下,您可以遵循类似于 this question.

的内容
histylim <- function(...){
    ff <- tempfile()
    png(filename=ff)
    res <- hist(...)
    corners <- par("usr")
    dev.off()
    unlink(ff)
    return(c(corners[3], corners[4]))
}

histylim(mtcars$mpg)
#> [1] -0.48 12.48
histylim(mtcars$disp)
#> [1] -0.28  7.28

以下内容不适合您

将参数 plot=FALSE 添加到直方图函数,需要 if/when,通过在 plot() 中调用它来绘制对象,如:

mtcars

p1 <- hist(mtcars$mpg, plot = FALSE)
plot(p1)