Sina PN Sina PN - 8 days ago 4x
R Question

Plot party decision tree

I have the following plot as you can see in the picture, Is there any way to see exact number of percentage in the leaf nodes?

enter image description here


If you want to "see" the percentages, the easiest way is to make a table() of the terminal nodes vs. the response and then look at the conditional proportions.

If you want to "see" the proportions in the barplot, then there was no possibility to do this up to now. However, I tweaked the node_barplot() function to accomodate this feature. So if you re-install the partykit package (successor of the party package) from R-Forge you can try it:

install.packages("partykit", repos = "")

For illustration I will just use the iris data:

ct <- ctree(Species ~ ., data = iris)
plot(ct, tp_args = list(text = TRUE))


By enabling the text = TRUE option you get the labels drawn above the bars (horizontally). An equivalent specification would be text = "horizontal" or text = "h". If you want a narrower layout you could also use:

plot(ct, tp_args = list(text = "vertical", ymax = 1.5))


And the frequency table is simply:

tab <- table(predict(ct, type = "node"), iris$Species)
prop.table(tab, 1) * 100
##         setosa versicolor  virginica
##   2 100.000000   0.000000   0.000000
##   5   0.000000  97.826087   2.173913
##   6   0.000000  50.000000  50.000000
##   7   0.000000   2.173913  97.826087