【问题标题】:adjust plot parameters in R while plotting regsubsets object in R (more room below x axis)在 R 中绘制 regsubsets 对象时调整 R 中的绘图参数(x 轴下方有更多空间)
【发布时间】:2014-02-18 17:04:51
【问题描述】:

我正在尝试调整通常使用par(mar=c(10,4.1,4.1,2.1) 执行的绘图参数,以便在 x 轴下方留出更多空间来绘制这些标签。现在变量名已经不在屏幕上了。

是不是我正在绘制的 jumps 包或 regsubsets 对象无法识别 par(mar=c(10,4.1,4.1,2.1))

这是我正在尝试做的一个简化示例。

require('leaps')
par(mar=c(10,4.1,4.1,2.1))
leaps <- regsubsets(mpg~disp+hp+drat+wt+qsec, data=mtcars, nbest=2, nvmax=5)
## artificially making labels longer... my labels are longer than this example dataset
labs <- sapply(leaps$xnames, function(x) paste(rep(x,5), collapse='')) 
plot(leaps, scale=c('adjr2'), labels=labs))

【问题讨论】:

    标签: r visualization regression


    【解决方案1】:

    这是由函数 (plot.regsubsets()) 在函数体内设置 mar 引起的。这会覆盖您设置的 mar

    您可以通过将 mar 参数添加到 plot.regsubsets() 函数并将其传递给函数体第 3 行的 par() 调用来解决此问题:

    plot.regsubsets<-function(x,labels=obj$xnames,main=NULL,
                              scale=c("bic","Cp","adjr2","r2"),
                              col=gray(seq(0,0.9,length=10)),mar = c(7,5,6,3)+0.1, ...){
        obj<-x
        lsum<-summary(obj)
        par(mar=mar)
        nmodels<-length(lsum$rsq)
        np<-obj$np
        propscale<-FALSE
        sscale<-pmatch(scale[1],c("bic","Cp","adjr2","r2"),nomatch=0)
        if (sscale==0)
            stop(paste("Unrecognised scale=",scale))
        if (propscale)
            stop(paste("Proportional scaling only for probabilities"))
    
        yscale<-switch(sscale,lsum$bic,lsum$cp,lsum$adjr2,lsum$rsq)
        up<-switch(sscale,-1,-1,1,1)
    
        index<-order(yscale*up)
    
        colorscale<- switch(sscale,
                            yscale,yscale,
                            -log(pmax(yscale,0.0001)),-log(pmax(yscale,0.0001)))
    
        image(z=t(ifelse(lsum$which[index,],
              colorscale[index],NA+max(colorscale)*1.5)),
              xaxt="n",yaxt="n",x=(1:np),y=1:nmodels,xlab="",ylab=scale[1],col=col)
    
        laspar<-par("las")
        on.exit(par(las=laspar))
        par(las=2)
        axis(1,at=1:np,labels=labels)
        axis(2,at=1:nmodels,labels=signif(yscale[index],2))
    
        if (!is.null(main))
            title(main=main)
        box()
        invisible(NULL)
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多