Plot decision tree in R Caret
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
The caret package in R provides a unified interface for training machine learning models, including decision trees. To plot a decision tree trained with caret, you extract the underlying model object and pass it to a visualization function. For rpart trees, use rpart.plot() from the rpart.plot package. For party trees, use plot() directly. Caret wraps the model fitting but does not provide its own plotting — you always work with the underlying model's plot functions.
Training a Decision Tree with Caret
Plotting with rpart.plot
The type and extra parameters control how much information appears on each node:
type | Description |
| 0 | Draw split labels on branches |
| 1 | Label all nodes with split info |
| 2 | Label with split and node number |
| 3 | Label with split condition at each node |
| 4 | Like type 3 but includes class label |
extra | Description |
| 0 | No extra info |
| 1 | Number of observations |
| 2 | Classification rate |
| 100 | Percentage of observations |
| 104 | Class probability and percentage |
Using the Built-in plot Function
Regression Tree Example
For regression trees, each leaf shows the predicted value (mean of observations in that node) instead of a class label.
Tuning and Plotting with Different Complexity
Lower cp values produce more complex trees (more splits). Cross-validation helps find the optimal cp that balances accuracy and simplicity.
Using party/ctree for Conditional Trees
ctree uses statistical tests (p-values) to determine splits, producing different trees than rpart.
Saving the Plot
Variable Importance
Common Pitfalls
- Accessing
modelinstead ofmodel$finalModel: Caret'strain()returns a caret model object, not an rpart object. Passingmodeldirectly torpart.plot()fails. Always extractmodel$finalModelfor the underlying rpart tree. - Not installing
rpart.plotseparately: Therpartpackage only provides the basicplot()function for trees. Therpart.plotpackage (a separate CRAN package) is needed for the detailed, publication-quality plots withrpart.plot(). - Tree is a single root node with no splits: If the complexity parameter
cpis too high, the tree is pruned to a single node. Lower thecpvalue in the tuning grid or settuneLengthto a larger number to explore more options. - Using
rpart.plotwith non-rpart models:rpart.plot()only works withrpartobjects. Trees fromctree,randomForest, orgbmrequire their own visualization functions. Checkmodel$methodto determine the model type. - Forgetting
set.seed()before training: Decision tree training with cross-validation is stochastic. Withoutset.seed(), you get different trees on each run, making results non-reproducible.
Summary
- Train a decision tree with
caret::train(method = "rpart")and extract the tree withmodel$finalModel - Plot with
rpart.plot(model$finalModel, type = 4, extra = 104)for detailed, readable trees - Use
typeandextraparameters to control the level of detail shown on nodes - For conditional inference trees, use
method = "ctree"and plot withplot(model$finalModel) - Use
varImp(model)to visualize which features the tree considers most important - Always set
set.seed()before training for reproducible results
Related reading
- Plot feature importance with xgboost
- Plot Interactive Decision Tree in Jupyter Notebook
- Plot k-Nearest-Neighbor graph with 8 features?
- Plot learning curves with caret package and R
- Plot logarithmic axes
- Plot multiple graphs in one plot using Tensorboard
- Plot yerr/xerr as shaded region rather than error bars
- Plotting a pie chart out of a dictionary

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.