[R] how to label the branches of a tree

David Winsemius dwinsemius at comcast.net
Wed Feb 25 19:41:48 CET 2009


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.

-- 
David Winsemius

 > sessionInfo()
R version 2.8.1 Patched (2009-01-07 r47515)
i386-apple-darwin9.6.0

locale:
en_US.UTF-8/en_US.UTF-8/C/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

other attached packages:
[1] rpart_3.1-42

loaded via a namespace (and not attached):
[1] grid_2.8.1      lattice_0.17-20 tools_2.8.1

 > capabilities()
     jpeg      png     tiff    tcltk      X11     aqua http/ftp   
sockets   libxml     fifo   cledit
     TRUE     TRUE    FALSE     TRUE     TRUE     TRUE     TRUE      
TRUE     TRUE     TRUE     TRUE
    iconv      NLS  profmem    cairo
     TRUE     TRUE    FALSE     TRUE


On Feb 25, 2009, at 1:01 PM, David Winsemius wrote:

> 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.
>
> -- 
> David Winsemius
>
> On Feb 25, 2009, at 10:44 AM, Utkarsh Singhal wrote:
>
>> Hi David,
>>
>> May be I was not clear, but I want to label the branches itself and  
>> not the branching points or the leaves.
>>
>> Regards
>> Utkarsh
>>
>> From: Philip Twumasi-Ankrah [mailto:nana_kwadwo_derkyi at yahoo.com]
>> Sent: Wednesday, February 25, 2009 9:07 PM
>> To: Utkarsh Singhal
>> Cc: R-help Forum
>> Subject: Re: [R] how to label the branches of a tree
>>
>> You may have to change/scale the sizes of the font by using "cex"  
>> and then to keep all labels within the plotting window, use  
>> "xpd=TRUE". Like in
>> text(fit, use.n=TRUE, cex=0.8, xpd=TRUE)
>> Philip
>> --
>> A Smile costs Nothing But Rewards Everything
>>                                                              -  
>> Anonymous
>>
>> Happiness is not perfected until it is shared
>>                                                              -Jane  
>> Porter
>>
>>
>> --- On Wed, 2/25/09, David Winsemius <dwinsemius at comcast.net> wrote:
>> From: David Winsemius <dwinsemius at comcast.net>
>> Subject: Re: [R] how to label the branches of a tree
>> To: "Utkarsh Singhal" <utkarshs at ambaresearch.com>
>> Cc: "R-help Forum" <r-help at r-project.org>
>> Date: Wednesday, February 25, 2009, 7:28 AM
>> 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.
>>
>>
>>
>> --David Winsemius
>>
>>
>>
>> Note:  your mailer is putting single quotes inside the enclosing  
>> angle brackets
>>
>> so my mailer does not recognize the r-help address as a valid  
>> address. Since
>>
>> this is the first time I have encountered this construction, I  
>> wonder if your
>>
>> mailer is adhering to standards in its reply-to functions.
>>
>>
>>
>>
>>
>>
>>
>> On Feb 25, 2009, at 1:19 AM, Utkarsh Singhal wrote:
>>
>>
>>
>> > Hi,
>>
>> >
>>
>> >
>>
>> >
>>
>> > I am using rpart package to fit classification trees.
>>
>> >
>>
>> >
>>
>> >
>>
>> > library(rpart)
>>
>> >
>>
>> > fit <- rpart(Kyphosis ~ Age + Number + Start, data=kyphosis)
>>
>> >
>>
>> > plot(fit,uniform=T)
>>
>> >
>>
>> > text(fit, use.n=TRUE)
>>
>> >
>>
>> >
>>
>> >
>>
>> > But I am unable to label the branches (not
>> the nodes) of the tree. Can
>>
>> somebody help me out in this?
>>
>> >
>>
>> >
>>
>> >
>>
>> > Thank you,
>>
>> >
>>
>> > Regards
>>
>> >
>>
>> > Utkarsh Singhal | Amba Research
>>
>> >
>>
>> > Ph +91 80 3980 8017 | Mob +91 99 0295 8815
>>
>> >
>>
>> > Bangalore * Colombo * London * New York * San José * Singapore *
>>
>> www.ambaresearch.com  <mailto:utkarshs at ambaresearch.com>
>>
>> >
>>
>> >
>>
>> >
>>
>> >
>>
>> >
>>
>> > This e-mail may contain confidential and/or privileged i... 
>> {{dropped:13}}
>>
>> >
>>
>> > ______________________________________________
>>
>> > R-help at r-project.org mailing list
>>
>> > https://stat.ethz.ch/mailman/listinfo/r-help
>>
>> > PLEASE do read the posting guide
>>
>> http://www.R-project.org/posting-guide.html
>>
>> > and provide commented, minimal, self-contained, reproducible code.
>>
>>
>>
>> ______________________________________________
>>
>> R-help at r-project.org mailing
>> list
>>
>> https://stat.ethz.ch/mailman/listinfo/r-help
>>
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>>
>> and provide commented, minimal, self-contained, reproducible code.
>>
>> This e-mail may contain confidential and/or privileged...{{dropped: 
>> 12}}
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.




More information about the R-help mailing list