Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to plot a large ctree() to avoid overlapping nodes

When I plotted the decision tree result from ctree() from party package, the font was too big and the box was also too big. They are overlapping other nodes.

Is there a way to customize the output from plot() so that the box and the font would be smaller ?

like image 827
JPC Avatar asked Dec 06 '12 20:12

JPC


2 Answers

The short answer seems to be, no, you cannot change the font size, but there are some good other options.

I know of three possible solutions. First, you can change other parameters in the plot to make it more compact. Second, you can write it to a graphic file and view that file. Third, you can use an alternative implementation of ctree() in the partykit package, which is a newer package by some of the same authors.

Default Plot Example

library(party)
airq <- subset(airquality, !is.na(Ozone))
airct <- ctree(Ozone ~ ., data = airq, 
               controls = ctree_control(maxsurrogate = 3))
plot(airct)  #default plot, some crowding with N hidden on leafs

default plot

Simplified plot

# simpler version of plot
plot(airct, type="simple",           # no terminal plots
  inner_panel=node_inner(airct,
       abbreviate = TRUE,            # short variable names
       pval = FALSE,                 # no p-values
       id = FALSE),                  # no id of node
  terminal_panel=node_terminal(airct, 
       abbreviate = TRUE,
       digits = 1,                   # few digits on numbers
       fill = c("white"),            # make box white not grey
       id = FALSE)
   )

enter image description here

This is somewhat better and one might be able to improve it further. To figure out these details, I originally did class(airct) which returned "BinaryTree". Armed with this info, I started reading ?plot.BinaryTree

Write to a file

A second simple solution is to write the plot to a file and then view the file. You may need to play with the settings to find the best fit.

png("airct.png", res=80, height=800, width=1600) 
   plot(airct) 
dev.off()

Plot with partykit package instead

Finally, you can use a newer and not-yet-finished re-implementation of the party package by some of the same authors. At this point (Dec 2012), the only function they have re-done is ctree(). This version allows you to change font size.

    library(partykit) 
    airct <- ctree(Ozone ~ ., data = airq)
    class(airct)  # different class from before
    # "constparty" "party"  
plot(airct, gp = gpar(fontsize = 6),     # font size changed to 6
  inner_panel=node_inner,
  ip_args=list(
       abbreviate = TRUE, 
       id = FALSE)
  )

enter image description here

Here I have left the leafs in their default setting because I have frankly never figured out how to get it to work the way I want. I suspect this has to do with the fact that the package is incomplete (as of Dec 2012). You can read about the plot method starting with ?plot.party

like image 188
MattBagg Avatar answered Nov 04 '22 17:11

MattBagg


Another option (that doesn't change what you want but does potentially solve the underlying problem) is to change the size of the figure itself, as I learned in my class for my assignment.

Replace the r in the below:

{r}

with:

{r, fig.width=X, fig.height=Y} 

where the X and Y need to be replaced by numbers chosen by you depending on what size you think works better.

This website, talks about doing this in more detail and universally throughout the document.

like image 23
user9894772 Avatar answered Nov 04 '22 19:11

user9894772