[R] rpart: plot without scientific notation

Stephen Milborrow milbo at sonic.net
Fri Aug 26 22:28:35 CEST 2011


Jay <josip.2000 at gmail.com> wrote:
> While I'm very pleased with the results I get with rpart and
> rpart.plot, I would like to change the scientific notation of the
> dependent variable in the plots into integers. Right now all my 5 or
> more digit numbers are displayed using scientific notation.

One way of getting rpart.plot to always display the response in integer 
format (i.e. no scientific notation) is to use sprintf like this:

library(rpart.plot)
library(earth)   # for the ozone1 data
data(ozone1)
ozone1$O3 <- 1000 * ozone1$O3   # for demo want big numbers
tree <- rpart(O3~., data=ozone1)
node.fun <- function(x, labs, digits, varlen)
{
    sprintf("%0.f", x$frame$yval)
}
rpart.plot(tree, node.fun=node.fun)

The code above uses its own node-label-formatting function, node.fun  For 
details see the prp help page and the rpart.plot vignette Chapter 5.  The 
"%0.f" tells sprintf to format as fixed point with zero digits after the 
decimal point.  The "x$frame$yval" is the predicted response at each node 
(look at tree$frame to get the idea).

I find sprintf easier to use than R's format function.  It's (arguably) less 
idiosyncratic, and works the same way across many languages, including C, 
C++, Python, Java, and R.  But as an alternative, with a bit of 
experimentation you may get format to do what you want, c.f.  the 
description of the "digits" argument on the prp help page.



More information about the R-help mailing list