2

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

Ian Campbell
  • 23,484
  • 14
  • 36
  • 57
  • 2
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Dec 04 '20 at 00:02
  • I attached the regression tree plot. I hope this helps – user10891362 Dec 04 '20 at 00:05
  • 2
    That doesn't allow us to run and test the code for possible solutions. – MrFlick Dec 04 '20 at 00:10
  • 1
    Maybe something like this: https://stackoverflow.com/questions/5963047/do-not-want-scientific-notation-on-plot-axis – Luca Angioloni Dec 04 '20 at 00:15
  • 1
    does options(scipen=999) do the trick? – zoowalk Dec 04 '20 at 00:20

1 Answers1

2

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

Ian Campbell
  • 23,484
  • 14
  • 36
  • 57