布局igraph对象作为风扇[R]

时间:2016-10-14 15:48:09

标签: r igraph

我有一个分层的非二进制树结构,表示为igraph个对象。

library(igraph)
df = data.frame(subregion = c("Africa", "Europe", "Asia", "Namibia", "Kenya", "Egypt", "Belgium", "China", "India"),
       region = c("World", "World", "World", "Africa", "Africa", "Africa", "Europe", "Asia", "Asia"))
df_graph = graph_from_data_frame(df, directed = F)
plot(df_graph, layout = layout_as_tree(df_graph, root = "World"))

enter image description here

有三个层次级别,同一级别的节点同样不同。现在我想将这个图形布局为扇形,就像你可以使用树形图一样:

df_graph_cl = cluster_fast_greedy(df_graph)
plot_dendrogram(df_graph_cl, type = "fan")

enter image description here

但是,节点之间的原始关系不会保留在树形图中,因为它是二进制树。关于如何将图形布局为扇形而不将其转换为树形图的任何想法?

1 个答案:

答案 0 :(得分:0)

我现在想出了一个解决方法并且认为我会分享它。关键是将图形转换为NEWICK格式。为此,我必须写一点function,在图表上执行深度优先搜索并创建NEWICK字符串。

使用示例数据,我现在可以轻松完成:

library(phytools)
newick = graph_to_newick(df_graph, root = "World")
df_tree = collapse.singles(read.newick(text = newick))
plot(df_tree, type = "fan")

产生所需的输出:

enter image description here