描绘供应网络结构

时间:2018-11-14 13:09:05

标签: r plot

我想绘制一个供应网络结构。我尝试使用igraph,但是直到现在还没有得出合理的结果。一个示例如下所示:

library(igraph)
d <- read.table(text = "V1 V2 weight
            s1  p1  88
            s3  p1  100
            s2  p2  100
            s3  p2  43
            p1  c1  21
            p1  c2  79
            p1  c3  88
            p2  c1  22
            p2  c2  121
            ", stringsAsFactors = F, header = T)

g <- graph_from_data_frame(d, directed = T)
plot(g, layout=layout.fruchterman.reingold,
     edge.width=E(g)$weight/20,
     vertex.shape = "none", vertex.label.font = 2,
     vertex.label.cex=1.1, edge.color="gray70")   

哪个给:

enter image description here

问题在于网络具有其他结构。一个合理的结果(除其他因素外)将显示(对于供应商)“ s”节点(位于左侧),“ p”节点(工厂)应位于中间,而“ c”节点(客户)应位于左侧。右侧。使用igraph甚至可以做到吗?有没有其他软件包可以做到这一点?

1 个答案:

答案 0 :(得分:2)

是的,这对于igraph是可行的。一种制作自己的布局的方法。一种简单的方法是将所有“ s”节点置于x = 1,将“ p”节点置于x = 2,将“ c”节点置于x = 3。每种类型(s,p,c)的每个不同节点应获得唯一的y值,以使它们不重叠。使用示例图:

LO = matrix(0, nrow=vcount(g), ncol=2)
LO[grep("s", V(g)$name), 1] = 1
LO[grep("p", V(g)$name), 1] = 2
LO[grep("c", V(g)$name), 1] = 3
LO[,2] = ave(rep(1, vcount(g)), LO[,1], FUN = seq_along) 

plot(g, layout=LO, edge.width=E(g)$weight/20, 
     vertex.shape = "none", vertex.label.font = 2,
     vertex.label.cex=1.1, edge.color="gray70") 

Tripartite Graph

此外,根据@Henrik的评论,您可以使用layout_with_sugiyama。您仍然需要定义(s,p,c)层。另外,杉山垂直地排列各层。您需要交换x和y坐标以获得水平布局。

Layers = rep(0,vcount(g))
Layers[grep("s", V(g)$name)] = 3
Layers[grep("p", V(g)$name)] = 2
Layers[grep("c", V(g)$name)] = 1
LO2 = layout_with_sugiyama(g, layers=Layers)$layout
LO2 = LO2[,2:1]

plot(g, layout=LO2, edge.width=E(g)$weight/20, 
     vertex.shape = "none", vertex.label.font = 2,
     vertex.label.cex=1.1, edge.color="gray70") 

Sugiyama layout