Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
按R中的颜色/子群对加权网络中的iGraph顶点进行分组_R_Networking_Igraph - Fatal编程技术网

按R中的颜色/子群对加权网络中的iGraph顶点进行分组

按R中的颜色/子群对加权网络中的iGraph顶点进行分组,r,networking,igraph,R,Networking,Igraph,我正在努力将我的网络按子组分组。我目前拥有以下网络: 我已经分配了这些子组。我想绘制聚集在一起的所有子组。要获得如下所示的图形: 大多数算法似乎基于图中的权重进行聚类。但是我想告诉它根据节点颜色/标记的子组进行聚类。这就是我现在要为这个网络编码的内容: #Graph with Weighted matrix g_weighted<-graph.adjacency(WeightedMatrix, mode="undirected", weighted = TRUE) #Make nod

我正在努力将我的网络按子组分组。我目前拥有以下网络:

我已经分配了这些子组。我想绘制聚集在一起的所有子组。要获得如下所示的图形:

大多数算法似乎基于图中的权重进行聚类。但是我想告诉它根据节点颜色/标记的子组进行聚类。这就是我现在要为这个网络编码的内容:

#Graph with Weighted matrix
g_weighted<-graph.adjacency(WeightedMatrix, mode="undirected", weighted = TRUE)

#Make nodes different colors based on different classes
numberofclasses<-length(table(ConnectedVertexColor))
V(g_weighted)$color=ConnectedVertexColor
Node_Colors <- rainbow(numberofclasses, alpha=0.5)
for(i in 1:numberofclasses){
 V(g_weighted)$color=gsub(unique(ConnectedVertexColor[i],Node_Colors[i],V(g_weighted)$color)
}
#Plot with iGraph
plot.igraph(g_weighted,
            edge.width=500*E(g_weighted)$weight,
            vertex.size=15, 
            layout=layout.fruchterman.reingold,  ##LAYOUT BY CLASS
            title="Weighted Network",
            edge.color=ifelse(WeightedMatrix > 0, "palegreen4","red4")
            )
legend(x=-1.5, y=-1.1, c(unique(ConnectedVertexColor)), pch = 19, col=Node_Colors, bty="n")
带加权矩阵的图
g_加权由于您没有提供数据,我根据您的“当前网络”图片进行猜测。当然,您需要的是图形的布局。下面我提供两个函数来创建可能满足您需求的布局

首先,一些看起来有点像你的数据

EL = structure(c(1, 5, 4, 2, 7, 4, 7, 6, 6, 2, 9, 6, 3, 10,
7, 8, 3, 9, 8, 5, 3, 4, 10, 13, 12, 12, 13, 12, 13, 15, 15,
11, 11, 14, 14, 11, 11, 11, 15, 15, 11, 11, 13, 13, 11, 13),
.Dim = c(23L, 2L))

g2 = graph_from_edgelist(EL, directed = FALSE)
Groups = c(rep(1, 10), 2,2,3,3,3)
plot(g2, vertex.color=rainbow(3)[Groups])

第一个布局

GroupByVertex01 = function(Groups, spacing = 5) {
         Position = (order(Groups) + spacing*Groups)
         Angle    = Position * 2 * pi / max(Position)
         matrix(c(cos(Angle), sin(Angle)), ncol=2)
}

GBV1 = GroupByVertex01(Groups)
plot(g2, vertex.color=rainbow(3)[Groups], layout=GBV1)
GroupByVertex02 = function(Groups) {
         numGroups = length(unique(Groups))
         GAngle    = (1:numGroups) * 2 * pi / numGroups
         Centers   = matrix(c(cos(GAngle), sin(GAngle)), ncol=2)
         x = y = c()
         for(i in 1:numGroups) {
                 curGroup = which(Groups == unique(Groups)[i])
                 VAngle = (1:length(curGroup)) * 2 * pi / length(curGroup)
                 x = c(x, Centers[i,1] + cos(VAngle) / numGroups )
                 y = c(y, Centers[i,2] + sin(VAngle) / numGroups)
         }
         matrix(c(x, y), ncol=2)
}

GBV2 = GroupByVertex02(Groups)
plot(g2, vertex.color=rainbow(3)[Groups], layout=GBV2)

第二布局

GroupByVertex01 = function(Groups, spacing = 5) {
         Position = (order(Groups) + spacing*Groups)
         Angle    = Position * 2 * pi / max(Position)
         matrix(c(cos(Angle), sin(Angle)), ncol=2)
}

GBV1 = GroupByVertex01(Groups)
plot(g2, vertex.color=rainbow(3)[Groups], layout=GBV1)
GroupByVertex02 = function(Groups) {
         numGroups = length(unique(Groups))
         GAngle    = (1:numGroups) * 2 * pi / numGroups
         Centers   = matrix(c(cos(GAngle), sin(GAngle)), ncol=2)
         x = y = c()
         for(i in 1:numGroups) {
                 curGroup = which(Groups == unique(Groups)[i])
                 VAngle = (1:length(curGroup)) * 2 * pi / length(curGroup)
                 x = c(x, Centers[i,1] + cos(VAngle) / numGroups )
                 y = c(y, Centers[i,2] + sin(VAngle) / numGroups)
         }
         matrix(c(x, y), ncol=2)
}

GBV2 = GroupByVertex02(Groups)
plot(g2, vertex.color=rainbow(3)[Groups], layout=GBV2)

如果您提供一个示例输入数据,以便我们可以运行和测试代码,那么就更容易为您提供帮助。在我看来,同一组中没有两个节点从未直接连接过。对吗?非常感谢!很抱歉没有提供数据。我正在努力生成一些随机的东西。但这正是我需要的!!!非常感谢。非常感谢。非常感谢。