[英]Grouping iGraph Vertices in a weighted network by color/subgroup in R
I am struggling to group my network by the subgroups. 我正在努力按小组将我的网络分组。 I currently have the following network:
我目前有以下网络:
Which I have assigned the subgroups. 我已经分配了这些子组。 I would like to plot all of the subgroups clustered together.
我想绘制聚类在一起的所有子组。 To get a graph that looks like this:
要获得如下所示的图形:
Most algorithms seems to cluster based on weights in the graph. 大多数算法似乎基于图中的权重进行聚类。 But I want to tell it to cluster based on the node colors/labelled subgroups.
但是我想告诉它基于节点颜色/标记的子组进行聚类。 This is what I have now to code this network:
这就是我现在要对该网络进行编码的内容:
#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")
The ConnectedVertexColor is a vector the contains information about if the node is a lipid, Nucleotide, Carb or AA. ConnectedVertexColor是一个向量,其中包含有关节点是否为脂质,核苷酸,碳水化合物或AA的信息。 I have tried the command
V(g_weighted)$community<-ConnectedVertexColor
but I cannot get this to transfer into useful information for iGraph. 我已经尝试过命令
V(g_weighted)$community<-ConnectedVertexColor
但是我无法将其转换为iGraph的有用信息。
Thanks for advice in advance. 预先感谢您的建议。
Since you do not provide data, I am making a guess based on your "Current Network" picture. 由于您未提供数据,因此我根据您的“当前网络”图片进行猜测。 Of course, what you need is a layout for the graph.
当然,您需要的是图形的布局。 Below I provide two functions to create layouts that might meet your needs.
下面,我提供两个函数来创建可能满足您需求的布局。
First, some data that looks a bit like yours. 首先,一些看起来像您的数据。
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])
First Layout 第一版面
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)
Second Layout 第二版面
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.