Search code examples
rrpart

How can I change plotted numbers from scientific notation to standard form in an rpart regression tree plot?


I am using the rpart.plot package to plot a regression tree. How can change the numbers from scientific notation into standard form at the bottom of the tree? For example, I want to change 14e+3 to 14000.

This is the code I am using:

library(rpart.plot)
rpart.plot(tm1, type=5, digits = 2, fallen.leaves = T)

And here is the result:

Regression Tree Example


Solution

  • This is a tricky one, because the package author decided to hard code in the scientific notation.

    If we review the source code, there is a function rpart.plot:::format0 that formats any values between 0.001 and 9999.

    So one approach, is to create a copy that over-writes those default values and assign it into the rpart.plot namespace.

    First, an example tree:

    library(nycflights13)
    library(rpart.plot)
    fit <- rpart(carrier ~ origin + distance, data = flights[flights$carrier %in% c("UA","AA","DL"),])
    rpart.plot(fit, type = 5, digits = 2, fallen.leaves = TRUE, extra = 101)
    

    enter image description here

    Note the scientific notation.

    Now, we'll create the copy and call it myformat0. We need to use ::: for a couple of functions because they aren't exported from rpart.plot. Note that I replaced the 9999 with 100000, but you can use whatever number you want.

    myformat0 <- function(x, digits=2)
    {
        rpart.plot:::check.integer.scalar(digits)
        if(digits == 0)
            digits <- getOption("digits")
        if(digits >= 0)
            rpart.plot:::formate(x, digits, smallest=.001, largest=100000)
        else
            sapply(x, format, digits=-digits)
    }
    

    Now we need to assign our custom function into the rpart.plot namespace. We can use assignInNamespace:

    assignInNamespace("format0", myformat0, ns = "rpart.plot")
    

    And now the plot:

    rpart.plot(fit, type = 5, digits = 2, fallen.leaves = TRUE, extra = 101)
    

    enter image description here