2017-04-05 34 views
1

enter image description here我開發了這個R-腳本來驅動決策流Rplot圖表,但我無法讓它顯示數值而不是科學記數法。我昨天花了一半的工作時間,試圖通過以下在stackoverflow上找到的示例使其成爲數字,但到目前爲止沒有運氣。詳情請參閱代碼和屏幕截圖。如何刪除Rplot圖表的科學記數法

#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」意味着從下面的照片? (我從一個項目中複製了這段代碼)。

回答

0

嘗試將digits = -2添加到代碼中的prp呼叫