繁体   English   中英

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

[英]Grouping iGraph Vertices in a weighted network by color/subgroup in R

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

当前网络

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

目标

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

#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")

ConnectedVertexColor是一个向量,其中包含有关节点是否为脂质,核苷酸,碳水化合物或AA的信息。 我已经尝试过命令V(g_weighted)$community<-ConnectedVertexColor但是我无法将其转换为iGraph的有用信息。

预先感谢您的建议。

由于您未提供数据,因此我根据您的“当前网络”图片进行猜测。 当然,您需要的是图形的布局。 下面,我提供两个函数来创建可能满足您需求的布局。

首先,一些看起来像您的数据。

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)

版式1

第二版面

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)

版面2

暂无
暂无

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

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