Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing label positions in a R party plot (decision/regression trees)

The partykit package give a nice representation of decision trees. The only problem I have with it is when labels are long and then they are overlapping. Is it possible to move those labels to prevent it (see the blue arrows on the picture below)?

library("rpart")
library("partykit")
rp <- rpart(Kyphosis ~ Age + Number + Start, data = kyphosis)
party_rp <- as.party(rp)

plot(party_rp)

enter image description here

like image 706
Jot eN Avatar asked Oct 18 '22 07:10

Jot eN


1 Answers

The default panel function for drawing the edge labels edge_simple implements a few justification strategies: Labels can either "alternate" across the edges, be "decreasing", "increasing", or "equal". However, these justification strategies are just employed starting from a minimum label length of justmin which defaults to Inf (i.e., no justification). See ?edge_simple for more details.

You wanted to see an example where justification is "increasing" and always applied (i.e., justmin = 1):

plot(party_rp,
  ep_args = list(justmin = 1, just = "increasing"))

edge_simple

like image 60
Achim Zeileis Avatar answered Oct 21 '22 05:10

Achim Zeileis