[R] Problem plotting output from tree()

Ian Robertson igr at stanford.edu
Sat Nov 14 00:08:38 CET 2009


Prof. Ripley kindly pointed out to me that the "digits" parameter in 
text.tree() is intended to work on numeric labels. The values that I 
wanted to modify are not labels (at least not the ones so controlled), 
nor are they numeric.

I have since figured out how to do what I want. The values I want to 
change are stored within the tree object as character strings. By taking 
these strings apart, rounding the numeric component, and putting them 
back together I have been able to solve my problem. I have modified my 
original code to show what I did:

####################################################
#some fake data
    set.seed(123)
    f1 <- factor(rep(1:4, 50))
    m1 <- matrix(runif(800), nc=4)
    d1 <- data.frame(f1, m1)
#the test
    library(tree)
    mdl1 <- tree(f1 ~ ., data=d1)
#(messy) output
    plot(mdl1); text(mdl1, cex=.6)
#minor surgery on the tree object
    s1 <- nchar(mdl1$frame$splits[, 1]) > 0
    sp1 <- mdl1$frame$splits[s1, 1]
    mdl1$frame$splits[s1, 1] <- paste("<", round(as.numeric(substr(sp1, 
2, nchar(sp1))), 2), sep="")
#(better) output
    plot(mdl1); text(mdl1, cex=.6)
####################################################

Ian Robertson




More information about the R-help mailing list