I am using the prp
function from the rpart.plot
package to plot a tree. For categorical data like states, it gives a really long list of variables and makes it less readable. Is there any way to wrap text to two or more lines if exceeds some length?
Asked
Active
Viewed 3,206 times
6
-
1check out this http://www.milbo.org/rpart-plot/prp.pdf You could create a function to cut down variable length – user2510479 Mar 24 '14 at 19:39
1 Answers
4
Here's an example that wraps long split labels over multiple lines. The maximum length of each line is 25 characters. Change the 25 to suit your purposes. (This example is derived from Section 6.1 in the rpart.plot vignette.)
tree <- rpart(Price/1000 ~ Mileage + Type + Country, cu.summary)
split.fun <- function(x, labs, digits, varlen, faclen)
{
# replace commas with spaces (needed for strwrap)
labs <- gsub(",", " ", labs)
for(i in 1:length(labs)) {
# split labs[i] into multiple lines
labs[i] <- paste(strwrap(labs[i], width=25), collapse="\n")
}
labs
}
prp(tree, split.fun=split.fun)

Stephen Milborrow
- 976
- 10
- 14