-1

I have plotted a tree and ideally, I want the tree to show all the labels of the variables. For example, show "Time Spent Reading" (my labels to the variable) instead of "time_reading" (the variable). Particularly, so far, I use the package rpart.plot:

tree1 <- ...
rpart.plot(tree1)

And all the nodes show the name of variables, instead of the labels. How could I add a label command to replace the variable names with the labels? Thank you in advance for your help!

Sherry Fu
  • 1
  • 1
  • 2
    See [here](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) and write a reproducible question. – deepseefan Nov 16 '19 at 02:37

1 Answers1

1

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)
Stephen Milborrow
  • 976
  • 10
  • 14