在 R/igraph 中使用 3 层(三方)可视化 graph/network

Visualizing graph/network with 3 layeres (tripartite) in R/igraph

我有一个 "layered" 网络,有 3 层,比方说 parents(P), children(C), grandchildren(G ).边缘总是指向年轻一代(patent->child、child->grandchild 或 parent->grandchild)。同一代的顶点之间没有边。 该图由 3 个边列表(P_C、C_G、P_C)表示。下面给出了一个简短的例子。

1) 这种 graph/network 的正确术语是什么?三方图?因此,我认为这是一个特殊情况,因为缺少后向连接。

2) 如何在 R (igraph) 中将其表示为图 object?

3) 我能否以描述 "layers" 的方式绘制图形,每个组 (P、C、GC) 的所有顶点都在相同的 x 坐标上对齐,从 P 开始左边,中间是C,右边是GC ?

4) 考虑到数据的分层性质,我可以检查具有这种结构的图之间的图同构吗? (我知道对于常规图形,它是 graph.isomorphic() 函数)。

edge_P_C <- read.table(text="P C
A B
A C", header=T)

edge_C_G <- read.table(text="C G
B D
B E
C F", header=T)

edge_P_G <- read.table(text="P G
A G", header=T)

1。学期

我想你可以说它是三方图,但我不确定这个术语是否用于有向图。

2。创建图表

要创建图形对象(使用 igraph 包)只需 rbind 所有边并使用 igraph.data.frame 创建它。在绑定之前,列名必须匹配。

all_edges <- do.call(rbind,
  lapply( list(edge_C_G, edge_P_C, edge_P_G), function(x) setNames(x, c("1","2")) )
)

g1 <- graph.data.frame(d = all_edges, directed = TRUE)

3。剧情

您需要在每个顶点上设置图层属性。据我了解,该层是由输入数据(三个表)隐式定义的:

v_layers_df <- unique( rbind(
  expand.grid( ID = edge_P_C$P, Layer = 1),
  expand.grid( ID = edge_P_G$P, Layer = 1),
  expand.grid( ID = edge_P_C$C, Layer = 2),
  expand.grid( ID = edge_C_G$C, Layer = 2),
  expand.grid( ID = edge_C_G$G, Layer = 3),
  expand.grid( ID = edge_P_G$G, Layer = 3)
))

v_layers <- setNames( v_layers_df$Layer, v_layers_df$ID)
V(g1)$layer <- v_layers[V(g1)$name]

通过顶点上的层属性,您可以在自己的布局函数中使用它(修改 Sugiyama):

layout.k_partite <- function(g) {
  l <- layout.sugiyama(g)$layout[,2:1]
  l[,1] <- V(g1)$layer
  l[,2] <- - l[,2] + 1 + max(l[,2])
  l
}

并这样使用:

plot(g1, layout = layout.k_partite(g1))

4。同构

graph.isomorphicigraph 包中的其他函数应该执行得很好。