0

I'm trying to get a feel for the ggraph package by following this demonstration: https://www.r-bloggers.com/plotting-trees-from-random-forest-models-with-ggraph/.

I would like to change the edge type to elbow and have tried replacing geom_edge_link() with geom_edge_elbow() but this returns the error "Error in FUN(X[[i]], ...) : object 'direction' not found".

Consulting the manual, I am guesssing this is because the object 'graph' is of class igraph and not dendrogram but I can't be sure and was wondering if someone could suggest a workaround?

Below is the code from the demo in the link above applied to the mtcars dataset:

library(randomForest)
library(igraph)
library(ggplot2)
library(ggraph)

data(mtcars)
mtcars_rf <- randomForest(mpg ~ ., data=mtcars, ntree=1000)

  # get tree by index
  tree <- randomForest::getTree(mtcars_rf, 
                                k = 100, 
                                labelVar = TRUE) %>%
    tibble::rownames_to_column() %>%
    # make leaf split points to NA, so the 0s won't get plotted
    mutate(`split point` = ifelse(is.na(prediction), `split point`, NA))

  # prepare data frame for graph
  graph_frame <- data.frame(from = rep(tree$rowname, 2),
                            to = c(tree$`left daughter`, tree$`right daughter`))

  # convert to graph and delete the last node that we don't want to plot
  graph <- graph_from_data_frame(graph_frame) %>%
    delete_vertices("0")

  # set node labels
  V(graph)$node_label <- gsub("_", " ", as.character(tree$`split var`))
  V(graph)$leaf_label <- as.character(tree$prediction)
  V(graph)$split <- as.character(round(tree$`split point`, digits = 2))

  dendro <- as.dendrogram(graph)
  # plot
  plot <- ggraph(graph, layout = 'dendrogram') + 
    theme_bw() +
    geom_edge_elbow() +
    geom_node_point() +
    geom_node_text(aes(label = node_label), na.rm = TRUE, repel = TRUE) +
    geom_node_label(aes(label = split), vjust = 2.5, na.rm = TRUE, fill = "white") +
    geom_node_label(aes(label = leaf_label, fill = leaf_label), na.rm = TRUE, 
                    repel = TRUE, colour = "white", fontface = "bold", show.legend = FALSE) 

  print(plot)

Running this throws the error: 'Error in FUN(X[[i]], ...) : object 'direction' not found'.

mkm
  • 11
  • 1
  • Welcome to StackOverflow. In order to ask a better question please read [How to ask a good question](https://stackoverflow.com/help/how-to-ask) and [Minimal, Complete, and Verifiable Example](https://stackoverflow.com/help/mcve) and [How to make a great R reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – Rui Barradas Dec 01 '18 at 09:02
  • Thanks @Rui Barradas, I've edited my code to add a reproducible example. – mkm Dec 01 '18 at 11:29
  • Try `geom_edge_elbow(aes(direction = "short"))`. The other option is `direction = "long"`. – Rui Barradas Dec 01 '18 at 12:49
  • You were missing some calls to `library`, which I have now included, and are missing a value for `tree_num`. – Rui Barradas Dec 01 '18 at 12:51
  • Thank you, I've tried both direction = "short" and direction = "long" to no avail. The node labels get printed but not the edges. – mkm Dec 01 '18 at 23:05

0 Answers0