rpart.plot (version 1.0-0)

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.

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
Deafult is TRUE meaing ``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 children's devi
digits
The number of significant digits in displayed numbers (actually only a suggestion, see format for details). Default 2. If 0, use
varlen
Length of variable names in text at the splits (and, for class responses, the class displayed at the node). Default -8, meaning truncate to eight characters. Three possibilities: =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 treated specially, meaning represent the factor lev
cex
Default NULL, meaning calculate the text size automatically. The default automatic calculation means that this seemingly innocuous argument has a far reaching effect. If necessary it will trigger the node shifting engine t
tweak
Adjust the (possibly automatically calculated) cex. Default 1, meaning no adjustment. Use say tweak=1.1 to make the text 10% larger. Note that font sizes are discrete, so the cex you ask f
compress
Default TRUE, meaning compress the tree horizontally by shifting nodes horizontally where space is available. (This is the same as plot.rpart's argument of the same name, except that here the default is TRUE<
ycompress
If TRUE (the default unless uniform=FALSE) and the initial automatically calculated cex is less than 0.7, shift crowded labels vertically where space is available. This often allows consid
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 DataLab