Basic dendrogram tutorial

Adapted from: https://www.r-graph-gallery.com/334-basic-dendrogram-with-ggraph/

library(ggraph)
library(igraph)
library(tidyverse)

Create the data

data=data.frame(
  level1="CEO",
  level2=c( rep("boss1",4), rep("boss2",4)),
  level3=paste0("mister_", letters[1:8])
)
data

Transform data frame to an edge list

edges_level1_2 = data %>% select(level1, level2) %>% unique %>% rename(from=level1, to=level2)
edges_level2_3 = data %>% select(level2, level3) %>% unique %>% rename(from=level2, to=level3)
edge_list=rbind(edges_level1_2, edges_level2_3)
edge_list

Basic graph

mygraph <- graph_from_data_frame( edge_list )
ggraph(mygraph, layout = 'dendrogram', circular = FALSE) + 
  geom_edge_diagonal() +
  geom_node_point() +
  theme_void()

Customizing

Here’s the data it’s using for layout. This is created on the fly by ggraph() above, but it can also be created separately.
The layout object prints as a data.frame, but it has other attributes, including the graph data.

ltdf = create_layout(mygraph, layout = 'dendrogram')
ltdf
class(ltdf)
## [1] "layout_igraph" "layout_ggraph" "data.frame"
attributes(ltdf)$graph
## IGRAPH 1b13907 DN-- 11 10 -- 
## + attr: name (v/c), ggraph.orig_index (v/n)
## + edges from 1b13907 (vertex names):
##  [1] CEO  ->boss1    CEO  ->boss2    boss1->mister_a boss1->mister_b
##  [5] boss1->mister_c boss1->mister_d boss2->mister_e boss2->mister_f
##  [9] boss2->mister_g boss2->mister_h

Labeling with data from the table

ggraph(mygraph, layout = 'dendrogram', circular = FALSE) + 
  geom_edge_diagonal() + 
  geom_node_label(aes(label = name)) +    # name is a column in the layout data frame
  theme_void()

Adding new data to plot

Modifying the ltdf layout data.frame with cbind() returns only a data.frame without the other attributes and classes that are needed to plot the graph. Instead, we’ll just assign a vector to a new name of the object, which leaves the rest of the attributes intact.

In the following example, we have larger, two line labels. These larger labels are too close to the default boundaries of the plot. We’ll modify the coordinate boundaries based on the coodinates seen in ltdf, as well as modify the text size and figure size.

ltdf$location = c("Texas", "Texas", "California", "Texas", "Oklahoma", "New Mexico", "Arizona", "California", "Oregon", "Nevada", "Washington")

ggraph(ltdf) + 
  geom_edge_diagonal() + 
  geom_node_label(aes(label = paste(name, location, sep = "\n")), size = 3.7) +  
  coord_cartesian(xlim = c(0.5,8.5), ylim = c(-0.2, 2.2)) +
  theme_void()