【问题标题】:R - Generate a plot without displaying itR - 生成一个图而不显示它
【发布时间】:2021-06-18 06:02:14
【问题描述】:

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

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

返回 ylim 的函数

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

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

这是我编写的用于返回一组直方图的函数

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

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

【问题讨论】:

    标签: r plot graphics histogram


    【解决方案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 添加到直方图函数中,如果/需要时,通过在plot() 中调用它来绘制对象,如下所示:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-12-16
      • 1970-01-01
      • 2018-04-23
      • 2019-09-05
      • 1970-01-01
      • 2020-06-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多