Plotting a tree hierarchy using ggraph

I have the following tree hierarchy data.frame:

hierarchy.df <- data.frame(raw=rep("Unclustered",26),
                           stringsAsFactors = F)

It describes an iterative clustering process where we start off with unclustered data (hierarchy.df$Unclustered), and in each iteration re-cluster each individual cluster from the previous iteration. Convergence is reach when no cluster can be broken down to finer clusters any more.

In this example convergence has been reached after 4 iterations.

I'm trying to plot this hierarchy using the ggraph package:

hierarchy.df$pathString <-,c(hierarchy.df,sep="/"))
hierarchy.graph <- data.tree::as.Node(hierarchy.df)
hierarchy.igraph <- data.tree::as.igraph.Node(hierarchy.graph)
igraph::V(hierarchy.igraph)$class <- names(igraph::V(hierarchy.igraph))


which gives me: enter image description here

So what's wrong here is that cluster 1.3 of iteration1 gets pointed upwards from the root rather than being leveled with all other clusters of that iteration and hence the tree looks bad.

Any idea how to fix this or to use another plotting package to do this in?

1 answer

  • answered 2018-07-11 23:25 Marius

    In your example plot, it looks like the wrong node has been set as the root. You can pass additional arguments to the layout algorithm in your ggraph() call, allowing you to explicitly specify what the root node should be:

                   root = "Unclustered")