rpart.plot
From rpart.plot v1.2-1
by Stephen Milborrow
Plot an rpart
model.
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
- Keywords
- partitioning, tree, CART, rpart, recursive
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 totext.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 forclass
objects; - under
- Applies only if
extra > 0
. DefaultFALSE
, meaning put the extra text in the box. UseTRUE
to put the text under the box. - clip.right.labs
- Default is
TRUE
meaning ``clip'' the right-hand split labels, i.e. do not printvariable=
. Applies only iftype=3
or4
. - fallen.leaves
- Default
FALSE
. IfTRUE
, 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) and1
(square shouldered branches). Default isif(fallen.leaves) 1 else .2
. - uniform
- If
TRUE
(the default), the vertical spacing of the nodes is uniform. IfFALSE
, 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
. If0
, usegetOption("digits")
. Details: Numbers from0.001
to9999
- 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
, meaningabbreviate
to three characters. Possible values are asvarlen
above, except that1
is trea - 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. Note that font sizes are discrete, so thecex
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 asplot.rpart
's argument of the same name, except that here the - ycompress
- If
TRUE
(the default unlessuniform=FALSE
), make more space by shifting labels vertically where space is available. Actually, this only kicks in if the initial automatically calculatedcex
is less than - snip
- Default
FALSE
. SetTRUE
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 ofprp
's arguments can be used.
Value
- The returned value is identical to that of
prp
.
See Also
prp
plot.rpart
text.rpart
rpart
Examples
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)
Community examples
Looks like there are no examples yet.