rpart.plot (version 1.2-2)

rpart.plot: Plot an rpart model.

Description

Plot an rpart model. This function combines and extends plot.rpart and text.rpart in the rpart package. It automatically scales and adjusts the displayed tree for best fit.

This is a front end to prp, with the most useful arguments of that function. See ../doc/prp.pdf for an overview.

Usage

rpart.plot(x=stop("no 'x' arg"),
    type=0, extra=0, under=FALSE, clip.right.labs=TRUE,
    fallen.leaves=FALSE, branch=if(fallen.leaves) 1 else .2,
    uniform=TRUE,
    digits=2, varlen=-8, faclen=3,
    cex=NULL, tweak=1,
    compress=TRUE, ycompress=uniform,
    snip=FALSE,
    ...)

Arguments

x
An rpart object. The only required argument.
type
Type of plot. Five possibilities:

0 The default. 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's

extra
Display extra information at the nodes. Possible values:

0 No extra information (the default).

1 Display the number of observations that fall in the node (per class for class objects;

under
Applies only if extra > 0. Default FALSE, meaning put the extra text in the box. Use TRUE to put the text under the box.
clip.right.labs
Default is TRUE meaning ``clip'' the right-hand split labels, i.e. do not print variable=. Applies only if type=3 or 4.
fallen.leaves
Default FALSE. If TRUE, display the leaves at the bottom of the graph.
branch
Controls the shape of the branch lines. Specify a value between 0 (V shaped branches) and 1 (square shouldered branches). Default is if(fallen.leaves) 1 else .2.
uniform
If TRUE (the default), the vertical spacing of the nodes is uniform. If FALSE, the nodes are spaced proportionally to the fit (more precisely, to the difference between a node's deviance and the sum of its two children's
digits
The number of significant digits in displayed numbers. Default 2. If 0, use getOption("digits"). Details: Numbers from 0.001 to 9999
varlen
Length of variable names in text at the splits (and, for class responses, the class in the node label). Default -8, meaning truncate to eight characters. Possible values: =0 use full names. >0 call
faclen
Length of factor level names in splits. Default 3, meaning abbreviate to three characters. Possible values are as varlen above, except that 1 is trea
cex
Default NULL, meaning calculate the text size automatically.
tweak
Adjust the (possibly automatically calculated) cex. Default 1, meaning no adjustment. Use say tweak=1.2 to make the text 20% larger. Note that font sizes are discrete, so the cex you ask f
compress
If TRUE (the default), make more space by shifting nodes horizontally where space is available. This often allows larger text. (This is the same as plot.rpart's argument of the same name, except that here the
ycompress
If TRUE (the default unless uniform=FALSE), make more space by shifting labels vertically where space is available. Actually, this only kicks in if the initial automatically calculated cex is less than
snip
Default FALSE. Set TRUE to interactively trim the tree with the mouse. See ../doc/prp.pdf (or just try it).
...
Extra arguments passed to prp and the plotting routines. Any of prp's arguments can be used.

Value

  • The returned value is identical to that of prp.

See Also

../doc/prp.pdf prp plot.rpart text.rpart rpart

Examples

Run this code
data(ptitanic)
tree <- rpart(survived ~ ., data=ptitanic, cp=.02)
                         # cp=.02 because want small tree for demo

old.par <- par(mfrow=c(2,2))
                         # put 4 figures on one page

rpart.plot(tree, main="default rpart.plot
(type = 0, extra = 0)")

prp(tree, main="type = 4, extra = 6", type=4, extra=6, faclen=0)
                         # faclen=0 to print full factor names

rpart.plot(tree, main="extra = 106,  under = TRUE", extra=106, under=TRUE, faclen=0)

# the old way for comparison
plot(tree, uniform=TRUE, compress=TRUE, branch=.2)
text(tree, use.n=TRUE, cex=.6, xpd=NA) # cex is a guess, depends on your window size
title("rpart.plot for comparison", cex=.6)

par(old.par)

Run the code above in your browser using DataCamp Workspace