Axis在ggraph树形图中标记标签

时间:2018-02-07 19:03:26

标签: r ggplot2 ggraph

我开始使用ggraph包,因为它在网络图的可用图中添加了额外的功能,看起来非常有前途(参见this tutorial)。然而,测试一些相当微不足道的东西,我已经卡住了。我基本上想要做的是重现一个简单的树形图:

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

dendrogram plot in base

但这就是它在ggraph中的表现:

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

ggraph dendrogram plot

而不是轴刻度标签,它只显示0,1,2等。我已经尝试了通常的ggplot2&#34;技巧&#34;但没有成功:

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

我错过了什么?

1 个答案:

答案 0 :(得分:1)

你可以尝试:

  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"))