One way of getting the variable names you want in the plotted tree is to change the names of the variables in the
original data. For example:
data(ptitanic)
mytitanic <- ptitanic
colnames(mytitanic) <- c("passenger class", "survived", "sex",
"age in years", "number siblings or spouses",
"number parents or children")
tree1 <- rpart(survived~., data=mytitanic)
rpart.plot(tree1)
Another way is to use the split.fun
argument. For details see the
vignette
for the rpart.plot package Section 6.1. For example:
data(ptitanic)
tree2 <- rpart(survived~., data=ptitanic)
split.fun <- function(x, labs, digits, varlen, faclen)
{
# replace variable names in the labels
labs <- sub("pclass", "passenger class", labs)
# labs <- sub("survived", "survived", labs)
# labs <- sub("sex", "sex", labs)
labs <- sub("age", "age in years", labs)
labs <- sub("sibsp", "number siblings or spouses", labs)
labs <- sub("parch", "number parents or children", labs)
labs # return the modified labels
}
rpart.plot(tree2, split.fun=split.fun)