3

Plotting a tree split on a factor variable results in a plot where the node is labeled with an index of the factor rather than the text of the level. How do I plot a tree where the label is the actual level of the factor variable?

For example, here is sample data on soccer players. The idea is that the country (through the goals variable) explains the income of the player. Country is use as a factor variable.

goals <- rpois(n = 5,lambda = 2)
income <- rnorm(100, goals*100000, sd = 1+goals*1000)
country <- rep(c("England","USA", "Nigeria", "Argentia", "Belgium"),
               times = 20)
player_df <- data.frame(income = income, country = country)

t1 <- rpart(income ~ ., 
      data = player_df, 
      cp = 0.01)

plot(t1); text(t1,)

Here is the plot that results:

enter image description here

goldisfine
  • 4,742
  • 11
  • 59
  • 83

1 Answers1

3

Try text(t1, pretty=FALSE). This will prevent the factor labels from being shortened.

eipi10
  • 91,525
  • 24
  • 209
  • 285