尝试重新创建网络时,如何将边缘权重分配给特定边缘?

时间:2019-09-30 00:30:35

标签: r networking igraph sna

因此,我的任务是重新创建特定的网络,如下所示: enter image description here

虽然我能够弄清楚如何处理节点,但是我不确定如何将每个边分配给宽度。我假设它应该类似于存储为矢量的节点颜色/形状来完成。但是,我怎么确切知道哪条边是什么?

library(sna)

hw2 <- rbind(
  # 1 2 3 4 5 6 7 8 932 1 2 3 4 8 0 2
  c(0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1),
  c(1,0,1,1,0,0,0,1,0,0,0,0,0,1,1,1,1),
  c(1,1,0,1,0,0,0,1,1,0,0,0,0,1,0,0,0),
  c(1,1,1,0,0,0,0,1,0,0,0,0,1,1,0,0,0),
  c(1,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0),
  c(1,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0),
  c(1,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0),
  c(1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0),
  c(1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0),
  c(1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0),
  c(1,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0),
  c(1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0),
  c(1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0),
  c(1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0),
  c(1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0),
  c(1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0),
  c(1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)
  )

#Labels
rownames(hw2) <- c("a1","a2","a3","a4","a5","a6","a7","a8","a9","a32","a11","a12","a13","a14","a18","a20","a22")
colnames(hw2) <- c("a1","a2","a3","a4","a5","a6","a7","a8","a9","a32","a11","a12","a13","a14","a18","a20","a22")

#Colors
net_col <-  c(rep("steelblue",8),rep("lightcoral",2),rep("steelblue",8))
edge_col <- c(rep("blue4",) )
#Shape
net_size <-c(rep(4,8),rep(50,2),rep(4,8))

#Network
net_hw2 <- network(hw2,matrix.type="adjacency")

#Graph 1 , similar to existing graph
gplot(net_hw2,vertex.col = net_col,usearrows = F,displaylabels = T,label.pos = 5,
      vertex.cex = 3, vertex.sides = net_size)

1 个答案:

答案 0 :(得分:1)

您可以使用gplot的edge.lwd参数为边缘分配宽度。为了匹配您的目标图,我还将使用edge.col分配颜色。唯一棘手的部分是弄清楚哪条边是哪条。我将为一个设置边缘宽度和颜色 边缘。之后,我将让您逐步调整所有其他参数。

您可以使用as.edgelist(net_hw2)获取所有边缘,这将基于节点的 number 给出边缘。您可以使用列表将节点号与其名称匹配 rownames(hw2)。我将以a3和a9之间的边缘为例。该边缘比其他大多数边缘都厚,并且是黄色而不是蓝色。查看rownames(hw2),我们看到a3是节点3,a9是节点9。通过as.edgelist(net_hw2)的输出,我们看到边缘29和51连接了这些节点。

我将创建一个颜色和宽度的矢量,以便默认颜色为海军蓝色,默认宽度为1。

LWD = rep(1, nrow(as.edgelist(net_hw2)))
COL = rep("navy", nrow(as.edgelist(net_hw2)))

现在我们可以显式更改连接a3和a9的边缘的颜色和宽度

LWD[29] = 8
COL[29] = "yellow"
LWD[51] = 8
COL[51] = "yellow"

现在您可以在情节中使用它

gplot(net_hw2,vertex.col = net_col,usearrows = F, displaylabels = T,
    label.pos = 5, vertex.cex = 3, vertex.sides = net_size, 
    edge.col=COL, edge.lwd=LWD)

graph with adjusted edge color and width

现在,我们在a3和a9之间有一个不错的黄色边缘。 您可以用相同的方式更改任何其他边缘的颜色和宽度。