如何将小数位数作为R plot.regsubsets中的参数传递?

问题描述 投票:0回答:1

在R中,plot.regsubsets的y轴始终四舍五入为两位数。我想定义自己要使用的位数。

从函数的来源来看,两个小数舍入是硬编码的(line 33)。

这些图调整y轴的位数最简单的方法是什么?

r plot visualization
1个回答
0
投票

每次启动R时都可以重新定义整个函数,也许可以使用额外的参数digits来扩展该函数:

plot.regsubsets<-function(x,labels=obj$xnames,main=NULL,
                          scale=c("bic","Cp","adjr2","r2"),
                          col=gray(seq(0,0.9,length=10)),digits=2,...){
    obj<-x
    lsum<-summary(obj)
    par(mar=c(7,5,6,3)+0.1)
    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],digits))

    if (!is.null(main))
        title(main=main)
    box()
    invisible(NULL)
}
© www.soinside.com 2019 - 2024. All rights reserved.