rpart plot text shorter

…衆ロ難τιáo~ 提交于 2019-12-22 06:37:48

问题


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?


回答1:


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)


来源:https://stackoverflow.com/questions/22618751/rpart-plot-text-shorter

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!