2

In the function rpart.plot (from the package rpart.plot, extension to the rpart package) there is the argument box.col, which controls the colour of the nodes in the tree. How do I set it such that it colours the nodes so that nodes of the same response are coloured the same?

I've tried a number of different variants with the box.col argument, such as using cols as factors, which ignores the colours selected. The closest I have so far is shown below


set.seed(1);x <- runif(100)
set.seed(2);y <- runif(100)

data <- matrix(c(x,y),ncol=2)
fact <- as.numeric(factor(--((x > 0.5 & y < 0.5))))
fact[x < 0.1] = 3
cols <- (c("grey80", "red", "blue"))


plot(data, col=fact)

t1 <- rpart(factor(fact) ~ data)
rpart.plot(t1, type=5, extra=2,
           box.col=cols)

Example failed plot I expect/want each matching response node to be coloured the same. In the given code, I would expect the '1' nodes to be grey80, the '2's to be red, and the '3' to be blue. The above plot shows what actually happens, which is not helpful.

As mentioned in the first part, how do I set it such that it colours the nodes so that nodes of the same response are coloured the same?

Beavis
  • 476
  • 3
  • 13

1 Answers1

2

how about using box.pallete attributes

rpart.plot(t1, type=5, extra=2,
           box.palette = list('grey80','red','blue'))

enter image description here

For more explanation, If you set the cols as factor in rpart.plot, cols will be assgined to each nodes sequentially.

For example,

>t1

1) root 100 29 1 (0.7100000 0.2200000 0.0700000)  # --> 'grey80'
   2) data1>=0.1037049 93 22 1 (0.7634409 0.2365591 0.0000000)  # --> 'red'
     4) data1< 0.5413779 47  0 1 (1.0000000 0.0000000 0.0000000) * # --> 'blue'
     5) data1>=0.5413779 46 22 1 (0.5217391 0.4782609 0.0000000)  # --> 'grey80'
      10) data2>=0.4849942 24  0 1 (1.0000000 0.0000000 0.0000000) * # --> 'red'
      11) data2< 0.4849942 22  0 2 (0.0000000 1.0000000 0.0000000) *# --> 'blue'
   3) data1< 0.1037049 7  0 3 (0.0000000 0.0000000 1.0000000) * # --> 'grey 80'
Steve Lee
  • 786
  • 3
  • 10