问题
I am struggling to group my network by the subgroups. I currently have the following network:
Current 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:
Goal
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. I have tried the command V(g_weighted)$community<-ConnectedVertexColor
but I cannot get this to transfer into useful information for iGraph.
Thanks for advice in advance.
回答1:
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)
来源:https://stackoverflow.com/questions/45720062/grouping-igraph-vertices-in-a-weighted-network-by-color-subgroup-in-r