Can't get ggraph to display labels for a dendrogram

Viewed 134

I use ggraph to visualise the results from a cluster analysis. But I can't get ggraph to display labels. I tried adding them as row names to the data, and I tried adding them as a vector to labels – to no avail. I'm very new to ggraph: Any help or criticism welcome.

MWE:

library(tidyverse)
library(cluster)
library(ggraph)

mat <- matrix(rnorm(n = 25, mean = 6, sd = 2.7))
rownames(mat) <- sample(LETTERS, size = 25, replace = FALSE)
dend_labels <- rownames(mat)

mat %>% 
  dist(method = "euclidean") %>% 
  agnes(metric = "ward") %>% 
  as.hclust() %>% 
  as.phylo() %>% 
  ggraph(layout = "dendrogram") +
  geom_edge_elbow() + 
  geom_node_label(aes(label = dend_labels))
1 Answers

Are you looking for this?

mat %>% 
  dist(method = "euclidean") %>% 
  agnes(metric = "ward") %>% 
  as.hclust() %>% 
  as.phylo() %>% 
  ggraph(layout = "dendrogram") +
  geom_edge_elbow() + 
  geom_node_label(aes(label = name, filter = leaf)) +
  theme_graph()

enter image description here

Related