理解一个简单的网络图

understanding a simple network chart

考虑这个简单的例子

mynodes_alt <- tibble(id = c(1,2,4,5),
                  mygroup = c(2,2,3,3))

myedges_alt <- tibble(from = c(1,1,4),
                  to = c(4,2,3),
                  power = c(3,3,3))

tbl_graph(nodes = mynodes_alt, edges = myedges_alt) 

# A tbl_graph: 4 nodes and 3 edges
#
# A rooted tree
#
# Node Data: 4 x 2 (active)
     id mygroup
  <dbl>   <dbl>
1     1       2
2     2       2
3     4       3
4     5       3
#
# Edge Data: 3 x 3
   from    to power
  <int> <int> <dbl>
1     1     4     3
2     1     2     3
3     4     3     3

如你所见,这里只有3条边。但是,使用 ggraph 创建网络可视化会生成一个令人费解的图表。

tbl_graph(nodes = mynodes_alt, edges = myedges_alt) %>%  
  ggraph(layout = 'grid') + 
  geom_node_point(aes(color = factor(mygroup))) +
  geom_edge_arc(aes(alpha = power, label = power)) + 
  geom_node_text(aes(label = id), repel = TRUE) +
  theme_graph()

这是怎么回事? 节点 5 来自哪里?它应该没有边缘。

谢谢!

事实证明根本没有错误!在内部,tidygraph 中的节点按行顺序索引!所以我的 ID 变量具有误导性。

在边数据中,从1到4的link实际上是指从第一行的节点到第四行的节点link[=16] =]的节点数据!