# Better decision tree graphics for rpart via party and partykit

R

Statistics

I've been using Graphviz to create better decision tree graphics "by hand" for `rpart`

objects created in `R`

(final tree). I stumbled on this post that shows how one could convert an `rpart`

object to a `party`

project via the `as.party`

function in `partykit`

to utilize the plot functions in `party`

. It looks quite nice.

I might have to do additional hacking as I like to display the node size and percentage of success in every node. For example, in `rpart`

, I do something like

```
```{r}
## rpartObj created from rpart
<-
textRpartCustom
{<- (ncol(yval) - 1L)/2
nclass <- yval[, 1L]
group <- yval[, 1L + (1L:nclass)]
counts if (!is.null(ylevel))
<- ylevel[group]
group <- rpart:::formatg(counts, digits)
temp1 if (nclass > 1) {
## temp1 <- apply(matrix(temp1, ncol = nclass), 1, paste,
## collapse = "/")
<- matrix(as.numeric(temp1), ncol=nclass)
temp1 ##temp1 <- paste("p=", round(temp1[, 2] / apply(temp1, 1, sum)*100, 1), "%", "; N=", apply(temp1, 1, sum), sep="")
<- paste("", round(temp1[, 2] / apply(temp1, 1, sum)*100, 1), "%", "; ", apply(temp1, 1, sum), sep="")
temp1
}if (use.n) {
<- paste(format(group, justify = "left"), "\n", temp1,
out sep = "")
}else {
<- format(group, justify = "left")
out
}return(out)
}
$functions$text <- textRpartCustom
rpartObjplot(rpartObj)
text(rpartObj)
```
```

to get these information to display for a classification fit.