在R/igraph中可视化具有3层(三方)的图/网络 [英] Visualizing graph/network with 3 layeres (tripartite) in R/igraph
问题描述
我有一个分为3层的分层"网络,比方说父母(P),孩子(C),孙子(G).边缘总是朝着年轻一代的方向(专利->孩子,孩子->孙子或父母->孙子).同一代中的顶点之间没有边. 该图由3个边缘列表(P_C,C_G,P_C)表示.下面是一个简短的例子.
I have a "layered" network, with 3 layers, let's say parents(P), children(C), grandchildren(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). A short example is given bellow.
1)这种图形/网络的恰当术语是什么?三方图?因此,我认为这是一个特殊的情况,因为缺少反向连接.
1) What is the proper term for this sort of graph/network? tripartite graph? As such, I suppose it is a particular case because of the lack of backward connections.
2)如何在R(igraph)中将其表示为图形对象?
2) How do I represent it as a graph object in R (igraph)?
3)我可以用描绘层"的方式绘制图形,使每个组(P,C,GC)的所有顶点都对齐在相同的x坐标上,从左侧的P开始,C在中间和GC的正确性?
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 ?
4)我是否可以检查具有这种结构的图之间的图同构性,并考虑到数据的分层性质. (我知道对于常规图,它将是graph.isomorphic()函数).
4) Can I check for graph isomorphisms between graphs with this structure, thaking into account the layered nature of the data. (I know for regular graphs it would be the graph.isomorphic() function).
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.学期
我想您可以说这是一个三方图,但是我不确定该术语是否用于有向图.
1. Term
I think you could say it is a tripartite graph but I am not sure if the term is used for directed graphs.
要创建图形对象(使用igraph程序包),只需rbind
所有边并使用igraph.data.frame创建即可.绑定之前,列名称必须匹配.
To create a graph object (with igraph package) just rbind
all the edges and create it with 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.情节
您需要在每个顶点上设置layer属性.据我了解,该层是由输入数据(三个表)隐式定义的:
3. Plot
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]
在顶点上具有layer属性,您可以在自己的布局函数(修改后的Sugiyama)中使用它:
With the layer attribute on the vertices you can use it in your own layout function (modified 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))
igraph
软件包中的graph.isomorphic
和其他功能应该可以正常工作.
The graph.isomorphic
and other functions from igraph
package should perform just fine.
这篇关于在R/igraph中可视化具有3层(三方)的图/网络的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!