rpart
model. A superset of rpart.plot
.rpart
model.
The arguments of this function are a superset of those of rpart.plot
.
See prp(x=stop("no 'x' arg"),
type=0, extra=0, under=FALSE, clip.right.labs=TRUE,
nn=FALSE, ni=FALSE, yesno=TRUE,
fallen.leaves=FALSE, branch=if(fallen.leaves) 1 else .2,
uniform=TRUE, left=TRUE, xflip=FALSE, yflip=FALSE,
Margin=0, space=1, gap=NULL,
digits=2, varlen=-8, faclen=3,
cex=NULL, tweak=1,
compress=TRUE, ycompress=uniform,
trace=FALSE, snip=FALSE, snip.fun=NULL, box.col=0, border.col=col,
round=NULL, leaf.round=NULL,
shadow.col=0, prefix="", suffix="", xsep=NULL,
under.font=font, under.col=1, under.cex=.8,
split.cex=1, split.font=2, split.family=family, split.col=1,
split.box.col=0, split.border.col=0,
split.lty=1, split.lwd=NULL, split.round=0,
split.shadow.col=0,
split.prefix="", right.split.prefix=NULL,
split.suffix="", right.split.suffix=NULL,
facsep=",", eq=" = ", lt=" < ", ge=" >= ",
branch.col=if(identical(branch.type, 0)) 1 else "gray",
branch.lty=1, branch.lwd=NULL,
branch.type=0, branch.tweak=1,
min.branch.width=.002, branch.fill=branch.col,
nn.cex=NULL, nn.font=3, nn.family="", nn.col=1,
nn.box.col=0, nn.border.col=nn.col,
nn.lty=1, nn.lwd=NULL, nn.round=.3,
node.fun=internal.node.labs,
split.fun=internal.split.labs,
FUN=text,
nspace=branch, minbranch=.3, do.par=TRUE,
add.labs=TRUE, clip.left.labs=FALSE, fam.main="",
yshift=0, yspace=space, shadow.offset=.4,
split.adj=NULL, split.yshift=0, split.space=space,
split.yspace=yspace, split.shadow.offset=shadow.offset,
nn.adj=.5, nn.yshift=0, nn.space=.8, nn.yspace=.5,
ygap=gap/2, under.ygap=.5, yesno.yshift=0,
xcompact=TRUE, ycompact=uniform, xcompact.ratio=.8, min.inter.height=4,
max.auto.cex=1, min.auto.cex=.15, ycompress.cex=.7, accept.cex=1.1,
shift.amounts=c(1.5, 2),
Fallen.yspace=.1, boxes.include.gap=FALSE,
...)
rpart
object. The only required argument. 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
0
No extra information (the default).
1
Display the number of observations that fall in the node
(per class for class
objects;
extra > 0
.
Default FALSE
, meaning put the extra text in the box.
Use TRUE
to put the text under the box.
See also under.cex
.TRUE
meaning ``clip'' the right split labels,
i.e. do not print variable=
.
Applies only if type=3
or 4
.
See also clip.left.labs
.FALSE
.
(In the current implementation
some overplotting may occur with nn=TRUE
.)frame
.
Default FALSE
.TRUE
, meaning write yes
and no
on the appropriate
sides of the top split.
Ignored if type=3
or 4
.
(Use nn.col
and the other nn
parameters
FALSE
. If TRUE
, display the leaves at the bottom of the graph.0
(V shaped branches) and
1
(square shouldered branches).
Default is if(fallen.leaves) 1 else .2
.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 TRUE
, meaning the left side of a split is the path
taken if the split condition is true.
With left=FALSE
the split labels are changed so the right side is true.FALSE
. If TRUE
, flip the tree horizontally.FALSE
. If TRUE
, flip the tree vertically, so the root
is at the bottom.0
, meaning no extra space.
To add say 10% space around the tree use Margin=0.1
.
(This is the margin
argument of plot.r
NULL
, meaning automatically choose a suitable value
(normally 1
, but if the graph is very crowded will be set to 0
2
.
If 0
, use getOption("digits")
.
Details:
Numbers from 0.001
to 9999
-8
, meaning truncate to eight characters.
Possible values:
=0 use full names.
>0 call
3
, meaning abbreviate
to three characters.
Possible values are as varlen
above, except that
1
is treaNULL
, meaning calculate the text size automatically.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 fTRUE
(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 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 thanFALSE
.
Use TRUE
to print the automatically calculated cex
, xlim
, and ylim
.
Use integer values greater than 1 for more detailed tracing.FALSE
.
Set TRUE
to interactively trim the tree with the mouse.
See snip=TRUE
.
Default NULL
, meaning no function.
Otherwise set snip.fun
to your own function
with the prototype function(tree)
, where tree
0
, meaning use the background color.col
, the color of the text in the box.
Use 0
for no border.
(Note: par
settings like col
can be passed in as ...arguments.
If not passNULL
, meaning calculate automatically.
Else specify 0
for sharp edges, and values greater
than 0
for rounded edges.
Bigger is more rNULL
, meaning use round
.
Else specify a value greater than or equal to 0
.0
, no shadow.
Try "gray"
or "darkgray"
.
(Note: overlapping shadows look better on devices that support alpha
""
.
Prepend this string to the node labels.
So could be the name of the fitted response, for instance.""
.
Append this string to the node labels.
Text after a double newline "\n\n"
(if any) will be plotted under the box.
(Actually, double newlines can be used in any of the prefix or suffix
arguextra>0
.
Default NULL meaning automatically select: usually
""
(two spaces), but "/ "
for rates.
Use xsep="/"
font
(which can be passed in as a ...argument).1
..8
, smaller than the text in the box.
The following control the split labels.cex
(which by default is calculated automatically).
Default 1
.2
, bold.
(Note: use font
to change the node label text.)""
,
or use something like split.family="serif"
.
(Note: use family
to change the node label text.)1
.
(Note: use col
to change the node label text.)0
, meaning use the background color.0
, invisible.1
, but the border will be invisible
unless you change the default split.border.col
.
(Note: use lty
to change the node box borders.)cex
(which by default is calculated automatically).
The border is by default invisible, see code{split.border.col}.0
, meaning sharp corners.
Else specify a value greater than or equal to 0
.
The split boxes are by default invisible,
see split.box.col
0
, no shadow.""
.
Prepend this string to the split labels.split.prefix
.
Prepend this string to the right split labels.
Applies only when type=3
or 4
.""
.
Append this string to the split labels.split.suffix
.
Append this string to the right split labels.
Applies only when type=3
or 4
.","
.
String which separates the factor levels in split labels.
See also xsep
, which separates the individual counts when extra
is used."= "
.
String which separates the factor name from the levels in split labels.
The idea is that you can add or remove spaces around the =
, or use
words if that suits you."< "
.
String which represents ``less than'' in split labels.">= "
.
String which represents ``greater than or equal'' in split labels.
The following control the branches.1
, but set to "gray"
if branch.type
is nonzero.1
.cex
(which by default is calculated automatically).
(Note: branch.lwd
does not control the width of the
``wide branches'' drawn when branch.type
is nonzero.)0
.
If nonzero draw ``wide branches'', with branch widths proportional
to the parameter selected by branch.type
as follows: 0
The default. The branch lines are drawn conventionally.
1
1
.
Applies only if branch.type
is nonzero.
Use this argument to scale the widths of the branches,
for example, branch.tweak=.5
to halve the width of the branches.
(By default, prp
0.002
.
Applies only if branch.type
is nonzero.
The minimum width of a branch, as a fraction of the page width.
The width of branches that would be thinner than min.branch.width
is clamped.
Incbranch.type
is nonzero.
Default branch.col
.
The following control the node numbers (with nn=TRUE)
.NULL
, meaning calculate the cex
of the node numbers automatically.
This and the following arguments apply only when nn=TRUE
.3
, italic.""
.1
.0
, meaning use the background color.nn.col
.1
.cex
(which by default is calculated automatically).
Default NULL
, meaning use lwd
(which can be passed in as a ...argument)..3
, meaning small corners.
Else specify a value greater than or equal to 0
.
The following are user definable functions.internal.node.labs
,
which specifies a function internal to prp
.
This is necessary for full support of extra
as described
in internal.split.labs
,
which specifies a function internal to prp
.
Otherwise set split.fun
to your own function with the
prototype f
text
.
The following are esoteric parameters, mostly for the graph layout engine.compress=TRUE
.
Default nspace=branch
.
The size of the space between a split and a leaf, relative to
the space between leaves.uniform=FALSE
. Default .3
.
The minimum height between levels is clamped at
minbranch
times the mean interlevel distance.
Needed because sometimes a split gives little or no improvTRUE
, meaning adjust the mar
parameter
so the tree fills the figure region.
This also sets xpd=NA
.
These graphic parameters are restored to their original
state before prp
exiTRUE
, meaning display the labels.
If FALSE
, gives a bare bones display similar to plot.rpart
.clip.right.labs
but for the left labels.
Default is FALSE
.
Note that clip.left.labs
and clip.right.labs
can be vectors,
indexed on the split number.""
.
The (inconsistent) name was chosen to minimize partial matching
with main
and family
which can be passed in as a
which can be passed in as ...arguments.0
.
Negative values move the text down; positive up
(the box around the text will follow along).1
.
Use this (and yspace
) for bigger boxes.
Since this affects the size of the (possibly invisible) boxes,
space
.
See the comments for space
..4
(but the shadow will be invisible unless the default shadow.col
is changed).adj
arguments.
Default NULL
, meaning use adj
(which defaults to 0.5
but can be passed in as a ...argu0
.
Negative values move the text down; positive up
(the box around the text will follow along).
This adjusts thespace
.
Affects the size of the box drawn around the text.
The split boxes are by default invisible
(see split.box.col
yspace
.shadow.offset
.
(but the shadow will be invisible unless the default shadow.col
is changed)..5
.0
..8
..5
.under=TRUE
or there is a double newline in prefix
or suffix
).
Vertical gap (in char heights) between the lower edge of the box and
the top of the text und0
.
Applies only when yesno=TRUE
.gap/2
.TRUE
(the default) and there is too much white space,
automatically change xlim
to compact the entire tree horizontally.
This usually only activates for small trees.
(The xcompact
and ycompact
TRUE
(the default) and there is too much vertical space,
automatically change ylim
to compact the entire tree vertically..8
.
Applies only when xcompact=TRUE
.
The maximum possible without overplotting is 1
,
but compacting by .8
usually
gives more pleasing spacing (it gives more space).4
.
Applies only when ycompact=TRUE
.
Minimum height (in units of character height)
between the lowest label in a layer and the highest label in the layer below it.cex
at this value
Default 1
, meaning never expand cex
, only contract when necessary..15
.
Never downscale to less than this when automatically calculating cex
,
even if overplotted labels result.
(The graph layout algorithm is unstable with cex
's below 0.15
--- mea.7
.
Applies only when ycompress=TRUE
.
Apply the ycompress
algorithm if the initial automatically calculated cex
is less than this.
The idea is that we don't want to shift if we get ancex
(because we don't want to shift if it gives only a small improvement in cex).
Default 1.1
i.e. require at least a 10% improvement.
Use 0
c(1.5, 2, 3)
.
For ycompress
, choose the best cex
yielded
by shifting nodes by these amounts,
in multiples of the box heights (after initial scaling)..1
, meaning allow 10% of the vertical space for the fallen leaves.
(The name Fallen.yspace
uses upper case to avoid partial matching with fallen.leaves
.)FALSE
.
Draw the boxes to include gap
and ygap
, for debugging.
This only affects the way the boxes are drawn, not the graph layout algorithm in any way.
With the optimum cex
at least opar
arguments. Only the ``important'' par
arguments are supported.
Note that arguments like col
apply only to the node labels.
To affect the split labels or brarpart
object.
Identical to the x
argument passed in unless snip
was used.NULL
unless snip
was used.cex
.cex
.rpart.plot
plot.rpart
text.rpart
rpart
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
prp(tree, main="default prp
(type = 0, extra = 0)")
prp(tree, main="type = 4, extra = 6", type=4, extra=6, faclen=0)
# faclen=0 to print full factor names
cols <- ifelse(tree$frame$yval == 1, "darkred", "green4")
# green if survived
prp(tree, main="assorted arguments",
extra=106, # display prob of survival and percent of obs
nn=TRUE, # display the node numbers
fallen.leaves=TRUE, # put the leaves on the bottom of the page
branch=.5, # change angle of branch lines
faclen=0, # do not abbreviate factor levels
trace=1, # print the automatically calculated cex
shadow.col="gray", # shadows under the leaves
branch.lty=3, # draw branches using dotted lines
split.cex=1.2, # make the split text larger than the node text
split.prefix="is ", # put "is " before split text
split.suffix="?", # put "?" after split text
col=cols, border.col=cols, # green if survived
split.box.col="lightgray", # lightgray split boxes (default is white)
split.border.col="darkgray", # darkgray border on split boxes
split.round=.5) # round the split box corners a tad
# 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("plot.rpart for comparison", cex=.6)
par(old.par)
Run the code above in your browser using DataLab