Learn R Programming

survMisc (version 0.4.6)

autoplot.rpart: Generate a ggplot for an rpart object

Description

Uses ggplot2 to render a recursive partitioning tree

Usage

autoplot(object, ...)

## S3 method for class 'rpart': autoplot(object, ..., title = "Recursive partitioning tree \\n Terminal nodes show fitted response", titSize = 20, uniform = FALSE, minbranch = 0.3, compress = FALSE, nspace, branch = 1, all = TRUE, nodeLabels = NULL, lineSize = 1, vArrows = FALSE, nodeSize = 5, nodeColor = "darkblue", leaf = c("fitR", "en", "both"), leafSize = 5, leafColor = "darkblue", digits = NULL, yNU = 0.02, yND = 0.02, yLD = 0.02)

Arguments

object
An object of class rpart, as returned by rpart::rpart()
...
Additional arguments (not implemented)
title
Title for plot
titSize
Title text size
uniform
The default is to use a non-uniform spacing proportional to the error in the fit. If uniform=TRUE, uniform vertical spacing of the nodes is used. This may be less cluttered when fitting a large plot onto a page.
minbranch
This parameter is ignored if uniform=TRUE. The usual tree shows branch lengths in proportion to improvement. Where improvement is minimal, there may be insuffcient room for node labels. minbranch sets the minimum lengt
compress
If compress=FALSE (the default), the leaves (terminal nodes) will be at the horizontal plot co-ordinates of $1:l$, the number of leaves. If compress=TRUE, the tree is arranged in a more compact form. (The compaction algo
nspace
Applies only when compress=TRUE. The amount of extra space between a node with children and a leaf, as compared to the minimal space between leaves. The default is the value of branch, below.
branch
Controls the shape of the branches from parent to child node. Needs to be in the range $0-1$. A value of $1$ gives square shouldered branches. A value of $0$ gives 'V' shaped branches, with other values being intermediate.
all
If all="FALSE" only terminal nodes (i.e. leaves) will be labelled
nodeLabels
These can be used to replace the names of the default labels from the fit. Should be given in the same order as those names
lineSize
Line size connecting nodes
vArrows
If vArrows=TRUE, adds vertical arrows for descending lines
nodeSize
Node text size
nodeColor
Node text color
leaf
If leaf="fitR" (the default) leaves are labelled with the fitted response. If this is a factor, the labels of the factor are used. The following apply only when the object is fit with rpart(..., m
leafSize
Leaf (terminal node) text size
leafColor
Leaf text color
digits
Number of significant digits for fitted response. Default is getOption("digits")
yNU
y value for Nodes moved Up. Used to prevent text from overlapping. This multiplier is applied to the difference of the largest and smallest $y$ values plotted. May need to be increased if larger text sizes are used in labelling node
yND
y value for Nodes moved Down. As above, but applies to text appearing below the node.
yLD
y value for Leaves moved Down. As above, but applies to text appearing below the leaves.

Value

  • A list with the following elmenents: [object Object],[object Object],[object Object]

Details

The plot shows a division at each node. This is read as right=TRUE. Thus for a node reading x > 0.5 the line descending to the right is that where x > 0.5.

Examples

Run this code
data("cu.summary", package="rpart")
fit <- rpart::rpart(Price ~ Mileage + Type + Country, cu.summary)
autoplot(fit)
data("stagec", package="rpart")
progstat <- factor(stagec$pgstat, levels = 0:1, labels = c("No", "Prog"))
cfit <- rpart::rpart(progstat ~ age + eet + g2 + grade + gleason + ploidy,
                     data = stagec, method = 'class')
autoplot(cfit)
set.seed(1)
df1 <- genSurvDf(model=FALSE)
r1 <- rpart::rpart(Surv(t1, e) ~ ., data=df1, method="exp")
autoplot(r1, leaf="en", title="Nodes show events / no. at risk")
autoplot(r1, compress=TRUE, branch=0.5, nspace=0.1,
         title="Nodes show events / no. at risk")
### oversize text; need to adjust 'y' values for text to compensate
a1 <- autoplot(r1, compress=TRUE, digits=5,
               nodeSize=10, yNU=0.05, yND=0.03,
               leafSize=10, , yLD=0.08, nodeLabels=seq(17))$plot
### can use expand_limits if text is cut off at margins
a1 + ggplot2::expand_limits(x=c(0.75, 7.25))

Run the code above in your browser using DataLab