Created on 21 June 2013
Revised on Sun Jun 23 00:54:02 2013
Plotting Classification Trees with the plot.rpart and rattle pckages
library(rpart) # Popular decision tree algorithm
library(rattle) # Fancy tree plot
library(rpart.plot) # Enhanced tree plots
library(RColorBrewer) # Color selection for fancy tree plot
library(party) # Alternative decision tree algorithm
library(partykit) # Convert rpart object to BinaryTree
library(caret) # Just a data source for this script, but probably one of the best R packages ever.
data(segmentationData) # Get some data
data <- segmentationData[,-c(1,2)]
tree.1 <- rpart(Class ~ .,data=data,control=rpart.control(minsplit=20,cp=0)) # Make big tree
plot(tree.1)
text(tree.1) # Will make a mess of the plot
prp(tree.1) # Will plot the tree
prp(tree.1,varlen=3) # Shorten variable names
new.tree.1 <- prp(tree.1,snip=TRUE)$obj # interactively trim the tree
prp(new.tree.1) # display the new tree
tree.2 <- rpart(Class ~ ., data) # A more reasonable tree
prp(tree.2) # A fast plot
fancyRpartPlot(tree.2) # A fancy plot from rattle