[R] rpart/tree issue

Stephen Milborrow milbo at sonic.net
Thu Sep 8 20:27:16 CEST 2011

"Brian Jensvold" <bjensvold at hawesfinancial.com> het geskryf
>I am trying to create a classification tree using either tree or rpart
>but when it comes to plotting the results the formatting I get is
>different than what I see in all the tutorials.  What I would like to
>see is the XX/XX format but all I get is a weird decimal value.  

The rpart.plot package provides more flexible routines for plotting
rpart trees than the rpart package itself.  Use rpart.plot with
extra=3 to get the XX/XX format you want:

tree <- rpart(survived ~ ., data=ptitanic)
rpart.plot(tree, extra=3)

See also page 4 of the vignette for the rpart.plot package.

>I was also wondering how you know which is yes and which is no in each
>leaf of the tree?  Is yes always on the left?

rpart.plot annotates the top label with Yes and No to remind viewers
that left is yes.  To flip that use

rpart.plot(tree, extra=3, left=FALSE)

But such flipping may not be a good idea -- if your audience is used
to plot.rpart they will expect yes on the left.

More information about the R-help mailing list