简体   繁体   English

在R /σ中用3层(三部分)可视化图形/网络

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

I have a "layered" network, with 3 layers, let's say parents(P), children(C), grandchildren(G). 我有一个“分层”网络,有3层,比方说父母(P),孩子(C),孙子(G)。 The edges are always directed and towards a younger generation (either patent->child, child->grandchild or parent->grandchild). 边缘总是朝向年轻一代(专利 - >儿童,儿童 - >孙子或父母 - >孙子)。 No edges between vertices in the same generation. 同一代中的顶点之间没有边。 The graph is represented by 3 edge lists (P_C, C_G, P_C). 该图由3个边列表(P_C,C_G,P_C)表示。 A short example is given bellow. 下面给出一个简短的例子。

1) What is the proper term for this sort of graph/network? 1)这种图形/网络的适当术语是什么? tripartite graph? 三方图? As such, I suppose it is a particular case because of the lack of backward connections. 因此,我认为这是一个特殊情况,因为缺乏反向连接。

2) How do I represent it as a graph object in R (igraph)? 2)如何在R(igraph)中将其表示为图形对象?

3) Can I plot the graph in a way that depicts the "layers", with all the vertices for each group (P,C,GC) aligned at the same x coordinates, going from P on the left, C in the middle and GC on the rigth ? 3)我可以用描绘“层”的方式绘制图形,每个组(P,C,GC)的所有顶点在相同的x坐标处对齐,从左边的P,中间的C和GC的严谨性?

4) Can I check for graph isomorphisms between graphs with this structure, thaking into account the layered nature of the data. 4)我可以检查具有这种结构的图之间的图同构,同时考虑数据的分层性质。 (I know for regular graphs it would be the graph.isomorphic() function). (我知道对于常规图形,它将是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. Term 1.期限

I think you could say it is a tripartite graph but I am not sure if the term is used for directed graphs. 我想你可以说这是一个三方图,但我不确定该术语是否用于有向图。

2. Create graph 2.创建图表

To create a graph object (with igraph package) just rbind all the edges and create it with igraph.data.frame. 要创建图形对象(使用igraph包),只需对所有边进行rbind并使用igraph.data.frame创建它。 Before binding the column names must match. 在绑定之前,列名必须匹配。

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. Plot 3.情节

You need to set the layer attribute on every vertex. 您需要在每个顶点上设置图层属性。 As I understand, the layer is implicitly defined by input data (three tables): 据我了解,该层由输入数据(三个表)隐式定义:

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]

With the layer attribute on the vertices you can use it in your own layout function (modified Sugiyama): 使用顶点上的layer属性,您可以在自己的布局函数中使用它(修改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
}

And use it this way: 并以这种方式使用它:

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

在此输入图像描述

4. Isomorphisms 4.同构

The graph.isomorphic and other functions from igraph package should perform just fine. 来自igraph包的graph.isomorphic和其他函数应该执行得很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM