rpart.plot
Plot an
Plot an rpart
model, automatically tailoring the plot
for the model's response type. For an overview, please see the package vignette
../doc/prp.pdf. This function is a simplified frontend to prp
,
with only the most useful arguments of that function, and
with different defaults for some of the arguments. The different defaults for the extra
and col.palette
arguments mean that this function automatically creates a colored plot
suitable for the type of model (whereas prp
by default
creates a minimal plot). In detail the different defaults are:
  rpart.plot 
  prp 
  
type 
  2 
  0 
 
extra 
  "auto" 
  0 
 
fallen.leaves 
  TRUE 
  FALSE 
 
varlen 
  0 
  8 
 
faclen 
  0 
  3 
 
box.palette 
  "auto" 
  0 
 
rpart.plot.version1
is compatible with
old versions of this function and has the same defaults as prp
.
 Keywords
 partitioning, tree, CART, rpart, recursive
Usage
rpart.plot(x=stop("no 'x' arg"),
type=2, extra="auto",
under=FALSE, fallen.leaves=TRUE,
digits=2, varlen=0, faclen=0,
cex=NULL, tweak=1,
snip=FALSE,
box.palette="auto", shadow.col=0,
...)
Arguments
 x

An
rpart
object. The only required argument.  type

Type of plot. Possible values:
0 Draw a split label at each split and a node label at each leaf.
1 Label all nodes, not just leaves. Similar to
text.rpart
'sall=TRUE
.2 Default. Like
1
but draw the split labels below the node labels. Similar to the plots in the CART book.3 Draw separate split labels for the left and right directions.
4 Like
3
but label all nodes, not just leaves. Similar totext.rpart
'sfancy=TRUE
. See alsoclip.right.labs
.  extra

Display extra information at the nodes. Possible values:
"auto" (case insensitive) Default. Automatically select a value based on the model type, as follows:
extra=106
class model with a binary responseextra=104
class model with a response having more than two levelsextra=100
other models0 No extra information.
1 Display the number of observations that fall in the node (per class for
class
objects; prefixed by the number of events forpoisson
andexp
models). Similar totext.rpart
'suse.n=TRUE
.2 Class models: display the classification rate at the node, expressed as the number of correct classifications and the number of observations in the node. Poisson and exp models: display the number of events.
3 Class models: misclassification rate at the node, expressed as the number of incorrect classifications and the number of observations in the node.
4 Class models: probability per class of observations in the node (conditioned on the node, sum across a node is 1).
5 Class models: like
4
but do not display the fitted class.6 Class models: the probability of the second class only. Useful for binary responses.
7 Class models: like
6
but do not display the fitted class.8 Class models: the probability of the fitted class.
9 Class models: the probabilities times the fraction of observations in the node (the probability relative to all observations, sum across all leaves is 1).
+100 Add
100
to any of the above to also display the percentage of observations in the node. For exampleextra=101
displays the number and percentage of observations in the node. Actually, it's a weighted percentage using theweights
passed torpart
.Note 1: Unlike
text.rpart
, by defaultprp
uses its own routine for generating node labels (not the function attached to the object). See thenode.fun
argument ofprp
. Note 2: Theextra
argument has special meaning formvpart
objects. See the Appendix to this package's vignette.  under

Applies only if
extra > 0
. DefaultFALSE
, meaning put the extra text in the box. UseTRUE
to put the text under the box.  fallen.leaves

Default
TRUE
to position the leaf nodes at the bottom of the graph. It can be helpful to useFALSE
if the graph is too crowded and the text size is too small.  digits

The number of significant digits in displayed numbers.
Default
2
. If0
, usegetOption("digits")
. If negative, use the standardformat
function (with the absolute value ofdigits
). Whendigits
is positive, the following details apply: Numbers from0.001
to9999
are printed without an exponent (and the number of digits is actually only a suggestion, seeformat
for details). Numbers out that range are printed with an ``engineering'' exponent (a multiple of 3).  varlen

Length of variable names in text at the splits
(and, for class responses, the class in the node label).
Default
0
, meaning display the full variable names. Possible values:0 use full names (default).
greater than 0 call
abbreviate
with the givenvarlen
.less than 0 truncate variable names to the shortest length where they are still unique, but never truncate to shorter than
abs(varlen)
.  faclen

Length of factor level names in splits.
Default
0
, meaning display the full factor names. Possible values are asvarlen
above, except that for backcompatibility withtext.rpart
the special value1
means represent the factor levels with alphabetic characters (a
for the first level,b
for the second, etc.).  cex

Default
NULL
, meaning calculate the text size automatically.  tweak

Adjust the (possibly automatically calculated)
cex
. Default1
, meaning no adjustment. Use saytweak=1.2
to make the text 20% larger. However, since font sizes are discrete thecex
you ask for may not be exactly thecex
you get. And a small change to tweak may not actually change the type size, or change it more than you want.  snip

Default
FALSE
. SetTRUE
to interactively trim the tree with the mouse. See the ../doc/prp.pdf (or just try it).  box.palette

Palette for coloring the node boxes based on the fitted value.
This is a vector of
colors
, for example box.palette=c("green", "green2", "green4"). Small fitted values are displayed with colors at the start of the vector; large values with colors at the end.The special value box.palette=0 (default for
prp
) uses the background color (typically white).The special value box.palette="auto" (default for
rpart.plot
, case insensitive) automatically selects a predefined palette based on the type of model.Otherwise specify a predefined palette e.g. box.palette="Grays" for the predefined gray palette (a range of grays). The predefined palettes are (see the
show.prp.palettes
function):Grays
Greys
Greens
Blues
Browns
Oranges
Reds
Purples
Gy
Gn
Bu
Bn
Or
Rd
Pu
(alternative names for the above palettes)BuGn
GnRd
BuOr
etc. (twocolor diverging palettes: any combination of two of the above palettes)RdYlGn
GnYlRd
BlGnYl
YlGnBl
(three color palettes)Prefix the palette name with
""
to reverse the order of the colors e.g.box.palette="auto"
orbox.palette="Grays"
.  shadow.col

Color of the shadow under the boxes.
Default
0
, no shadow. Try"gray"
or"darkgray"
.  …

Extra arguments passed to
prp
and the plotting routines. Any ofprp
's arguments can be used.
Value
The returned value is identical to that of prp
.
See Also
The package vignette ../doc/prp.pdf
prp
rpart.plot.version1
Functions in the rpart
package:
plot.rpart
text.rpart
rpart
Examples
old.par < par(mfrow=c(2,2)) # put 4 figures on one page
data(ptitanic)
#
binary.model < rpart(survived ~ ., data=ptitanic, cp=.02)
# cp=.02 for small demo tree
rpart.plot(binary.model,
main="titanic survived\n(binary response)")
rpart.plot(binary.model,
box.palette="Grays", # override default GnBu palette
main="titanic survived\nbox.palette = \"Grays\"")
#
anova.model < rpart(Mileage ~ ., data=cu.summary)
rpart.plot(anova.model,
shadow.col="gray", # add shadows just for kicks
main="miles per gallon\n(continuous response)\n")
#
multi.class.model < rpart(Reliability ~ ., data=cu.summary)
rpart.plot(multi.class.model,
main="vehicle reliability\n(multi class response)")
par(old.par)