79289602

Date: 2024-12-17 23:23:22
Score: 0.5
Natty:
Report link

You need to extract the leaf labels from the hierarchical clustering object and map them to the nodes. geom_node_text(aes(label = ifelse(leaf, label, NA))) places labels only on leaf nodes by checking the leaf column.

library(ggraph)
library(tidygraph)
library(igraph)

# Perform hierarchical clustering
hcity.D2 <- hclust(UScitiesD, "ward.D2")
#plot(hcity.D2)

# Convert hclust object to a tidygraph
graph <- as_tbl_graph(hcity.D2)

# Create the circular dendrogram with labels for leaf nodes
ggraph(graph, layout = "dendrogram", circular = TRUE) +
  geom_edge_link() +
  geom_node_text(aes(label = ifelse(leaf, label, NA)), size = 3, repel = TRUE) +
  coord_equal() +
  theme_void()

out

Reasons:
  • Probably link only (1):
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Low reputation (0.5):
Posted by: G-Man