如何删除Rplot图表的科学记数法

时间:2017-04-05 14:26:57

标签: scientific-notation rscript rpart

enter image description here我开发了这个R脚本来驱动决策流Rplot图表,但我不能让它显示数值而不是科学记数法。昨天我花了一半的工作时间试图通过我在stackoverflow上找到的例子来使它成为数字,但到目前为止还没有运气。有关详细信息,请参阅代码和屏幕截图。

#automatically convert columns with few unique values to factors
convertCol2factors<-function(data, minCount  =  3)
{
  for (c in 1:ncol(data))
    if(is.logical(data[, c])){
      data[, c]  =  as.factor(data[, c])
    }else{
      uc<-unique(data[, c])
      if(length(uc) <=  minCount)
        data[, c]  =  as.factor(data[, c])
    }
  return(data)
}

#compute root node error 
rootNodeError<-function(labels)
{
  ul<-unique(labels)
  g<-NULL
  for (u in ul) g  =  c(g, sum(labels == u))
  return(1-max(g)/length(labels))
}
# this function is almost identical to fancyRpartPlot{rattle} 
# it is duplicated here because the call for library(rattle) may trigger GTK load, 
# which may be missing on user's machine 
replaceFancyRpartPlot<-function (model, main  =  "", sub  =  "", palettes, ...) 
{

  num.classes <- length(attr(model, "ylevels"))

  default.palettes <- c("Greens", "Blues", "Oranges", "Purples", 
                        "Reds", "Greys")
  if (missing(palettes)) 
    palettes <- default.palettes

  missed <- setdiff(1:6, seq(length(palettes)))
  palettes <- c(palettes, default.palettes[missed])
  numpals <- 6
  palsize <- 5
  pals <- c(RColorBrewer::brewer.pal(9, palettes[1])[1:5], 
            RColorBrewer::brewer.pal(9, palettes[2])[1:5], RColorBrewer::brewer.pal(9, 
                                                                                    palettes[3])[1:5], RColorBrewer::brewer.pal(9, palettes[4])[1:5], 
            RColorBrewer::brewer.pal(9, palettes[5])[1:5], RColorBrewer::brewer.pal(9, 
                                                                                    palettes[6])[1:5])
  if (model$method  ==  "class") {
    yval2per <- -(1:num.classes) - 1
    per <- apply(model$frame$yval2[, yval2per], 1, function(x) x[1 + 
                                                                   x[1]])
  }
  else {
    per <- model$frame$yval/max(model$frame$yval)
  }
  per <- as.numeric(per)
  if (model$method  ==  "class") 
    col.index <- ((palsize * (model$frame$yval - 1) + trunc(pmin(1 + 
                                                                   (per * palsize), palsize)))%%(numpals * palsize))
  else col.index <- round(per * (palsize - 1)) + 1
  col.index <- abs(col.index)
  if (model$method  ==  "class") 
    extra <- 104
  else extra <- 101
  rpart.plot::prp(model, type  =  2, extra  =  extra, box.col  =  pals[col.index], 
                  nn  =  TRUE, varlen  =  0, faclen  =  0, shadow.col  =  "grey", 
                  fallen.leaves  =  TRUE, branch.lty  =  3, ...)
  title(main  =  main, sub  =  sub)
}

###############Upfront input correctness validations (where possible)#################

pbiWarning<-""
pbiInfo<-""

dataset <- dataset[complete.cases(dataset[, 1]), ] #remove rows with corrupted labels
dataset  =  convertCol2factors(dataset)
nr <- nrow( dataset )
nc <- ncol( dataset )
nl <- length( unique(dataset[, 1]))

goodDim <- (nr  >=minRows && nc  >= 2 && nl  >= 2)


##############Main Visualization script###########
set.seed(randSeed)
opt  =  NULL
dtree  =  NULL

if(autoXval)
  xval<-autoXvalFunc(nr)

dNames <- names(dataset)
X <- as.vector(dNames[-1])

form <- as.formula(paste('`', dNames[1], '`', "~ .", sep  =  ""))

# Run the model
if(goodDim)
{
  for(a in 1:maxNumAttempts)
  {
    dtree <- rpart(form, dataset, control  =  rpart.control(minbucket  =  minBucket, cp  =  complexity, maxdepth  =  maxDepth, xval  =  xval)) #large tree
    rooNodeErr <- rootNodeError(dataset[, 1])
    opt <- optimalCPbyXError(as.data.frame(dtree$cptable))

    dtree<-prune(dtree, cp  =  opt$CP)
    if(opt$ind > 1)
      break;
  }
}

#info for classifier
if( showInfo && !is.null(dtree) && dtree$method  ==  'class')
  pbiInfo <- paste("Rel error  =  ", d2form(opt$relErr * rooNodeErr), 
                 "; CVal error  =  ", d2form(opt$xerror * rooNodeErr), 
                 "; Root error  =  ", d2form(rooNodeErr), 
                 ";cp  =  ", d2form(opt$CP, 3), sep  =  "")

if(goodDim && opt$ind>1)
{
  #fancyRpartPlot(dtree, sub  =  pbiInfo)
  replaceFancyRpartPlot(dtree, sub  =  pbiInfo)


}else{
  if( showWarnings )
    pbiWarning <- ifelse(goodDim, paste("The tree depth is zero. Root error  =  ", d2form(rooNodeErr), sep  =  ""), 
                                     "Wrong data dimensionality" )

  plot.new()
  title( main  =  NULL, sub  =  pbiWarning, outer  =  FALSE, col.sub  =  "gray40" )
}
remove("dataset")

另外,如何从下面的照片中说出“n”的含义? (我从项目中复制了这段代码)。

1 个答案:

答案 0 :(得分:0)

尝试将digits = -2添加到代码中的prp来电

相关问题