Search code examples
rggplot2plottreerpart

How to change tree object class or plot nicely trees


I need to plot a nice tree for a project. The problem is that I have to use a specific function to create the tree that creates a tree datatype and all the functions used to plot trees in a nice way (ggtree, rpart.plot) require other types of object. Could you please help me to change the object data type of the tree or suggest me other functions to plot them? I have to plot prune.result. I cannot use other functions to create and prune the tree. (I know I can use the simply plot function but I use the ggplot package in all the project so I would like something similar to that).

Thank you in advance


dataframe <- data_frame(y = as.factor(rbinom(1000, size = 1, prob = 0.3)), x0 = rnorm(1000), x1 = rnorm(1000, 3), x2 = rnorm(1000, 3,0.5),  x3 = rnorm(1000, 1,0.5), x4 = rnorm(1000, 3.5), x5 = rnorm(1000, 2.3,0.5),  x6 = rnorm(1000, 1,0.5), x7 = as.factor(rbinom(1000, size = 4, prob = 0.5)), x8 = as.factor(rbinom(1000, size = 3, prob = 0.8)))

library(tree)
tree = tree::tree(y ~., data=dataframe, split='gini', control = tree.control(nobs=dim(dataframe)[1],mincut=1,minsize=2))
prune.result = prune.misclass(tree, best =12)

I tried to transform the prune.result into a dataframe to use it into ggtree and I also tried to convert it into an rpart object but I didn't manage.


Solution

  • If you need to work with tree objects produced by the tree package, but you want ggplot output, you should check out ggdendro

    Here's a full reprex based on your example:

    set.seed(1)
    
    dataframe <- data.frame(y = as.factor(rbinom(1000, size = 1, prob = 0.3)), 
                            x0 = rnorm(1000), 
                            x1 = rnorm(1000, 3), 
                            x2 = rnorm(1000, 3,0.5),  
                            x3 = rnorm(1000, 1,0.5), 
                            x4 = rnorm(1000, 3.5), 
                            x5 = rnorm(1000, 2.3,0.5),  
                            x6 = rnorm(1000, 1,0.5), 
                            x7 = as.factor(rbinom(1000, size = 4, prob = 0.5)), 
                            x8 = as.factor(rbinom(1000, size = 3, prob = 0.8)))
    
    library(tree)
    library(ggdendro)
    
    tree <- tree(y ~ ., data = dataframe, split = 'gini', 
                      control = tree.control(nobs = nrow(dataframe),
                                             mincut = 1, minsize = 2))
    
    prune.result <- prune.misclass(tree, best = 12)
    
    ggdendrogram(prune.result, theme_dendro = FALSE)
    

    Created on 2022-03-28 by the reprex package (v2.0.1)