Search code examples
rggplot2ggraph

Axis ticks labels in ggraph dendrogram


I started playing with the ggraph package since it looks very promising in terms of extra features it adds to the available plots of network graphs (see this tutorial). However, testing something fairly trivial, I'm already stuck. What I basically want to do is reproduce a simple dendrogram:

ArrestsDen <- as.dendrogram(hclust(dist(USArrests[1:5,])))
plot(ArrestsDen)

dendrogram plot in base

But this is how it looks in ggraph:

library(ggraph)
ggraph(ArrestsDen, 'dendrogram') +
  geom_edge_elbow()

ggraph dendrogram plot

Instead of axis tick labels, it shows just 0,1,2 etc.. I already tried the usual ggplot2 "tricks" but without success:

ggraph(ArrestsDen, 'dendrogram') +
  geom_edge_elbow() +
  scale_x_discrete(labels = labels)

# create labels manually
labs <- labels(ArrestsDen)
names(labs) <- as.character(1:length(labels(ArrestsDen)))
class(labs)

ggraph(ArrestsDen, 'dendrogram') +
  geom_edge_elbow() +
  scale_x_discrete(labels = labs)

ggraph dendrogram plot2

What am I missing?


Solution

  • You could try that:

      ggraph(ArrestsDen, 'dendrogram') +
        geom_edge_elbow() + 
        theme_bw() + 
        scale_x_continuous(breaks = c(0, 1, 2, 3, 4), label=c("Ark","Arizona","Cal", "Alab", "Alaska"))