Skip to content

how to label the branches of a tree

7 messages · Brian Ripley, Philip Twumasi-Ankrah, Utkarsh Singhal +1 more

#
When I execute that code, I get labels both on the branching points  
and at the leaves of the tree. So I guess my reply is "cannot  
reproduce" or "do not understand".  They are however not properly  
positioned and the edges and bottoms of some of the labels are cut  
off, but that did not sound like your question.

Using Mac OS X 10.5.6 and the ATT binary of R2.8.1. ?rpart? version  
3.1-42.
#
On Wed, 25 Feb 2009, David Winsemius wrote:

            
Try par(xpd), as mentioned in the help (?text.rpart): that also 
mentions a lot more ways to control the plot's appearance.
The graphics device matters too.

  
    
#
How do you want the labels to appear? Each branch will be the logical  
conjunction of the criteria for *all* of the upstream splits. Perhaps  
you can get direction by looking at the code of text.rpart:

getAnywhere(text.rpart)

Any split would need to add an "& Split-N yes" or an "& Split-N No" to  
any lower branch.
#
Following Dr Ripley's advice I looked at the text.rpart documentation  
a bit more thoroughly and after seeing "The edges connecting the nodes  
are labeled by left and right splits." I find that the fancy=TRUE  
invocation does cause a labeling of what you appear to be calling  
"branches".

See if this minor modification of the rpart example code produces your  
desired result:

library(rpart)
fit <- rpart(Kyphosis ~ Age + Number + Start, data=kyphosis)
par(mfrow=c(1,1), xpd=NA)  # otherwise on some devices the text is  
clipped
plot(fit)
text(fit, use.n=TRUE, fancy=TRUE)

On my "devices" (Mac OS X 10.5.6, R 2.8.1, quartz) the boxes around  
the leaf labels are a bit too low on the default screen device, and  
sending the output to a pdf file does not correct that problem, but as  
always the code is there for you to work with.