Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
866 views
in Technique[技术] by (71.8m points)

r - (igraph) Grouped layout based on attribute

I'm using the iGraph package in R to layout a network graph, and I would like to group the vertex coordinates based on attribute values.

Similar to the answered question How to make grouped layout in igraph?, my question differs in that the nodes needn't be grouped by a community membership that was derived from a community detection algorithm.

Rather, I want to layout with groups based on attribute values that are known in advance for each vertex.

For example, if each vertex has an attribute "Master.Org", and there are ~10 to ~20 distinct values for Master.Org, then how can I layout the graph such that all vertices within the same Master.Org are grouped ?

Thanks!

Additional Detail

In fact, two separate attributes provide nested levels of grouping.

My goal is to layout a graph object such that the "Master.Org" and "Org.Of" values are grouped together in their XY coordinates on the graph.

For example, each node will belong to an "Org.Of". And there can be multiple "Org.Of" values within the "Master.Org".

Thoughts ? Thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

While this question is rather old, it is a reasonable question and deserves an answer.

No data was provided so I will generate an arbitrary example.

library(igraph)
set.seed(1234)
G = erdos.renyi.game(20, 0.25)
V(G)$Group1 = sample(3,20, replace=TRUE)
plot(G, vertex.color=rainbow(3, alpha=0.4)[V(G)$Group1])

Original Graph

Without doing anything, the Group is ignored.

Now, we need to create a layout that will plot nodes in the same group close together. We can do this by creating a graph with the same nodes, but with additional links between nodes in the same group. The within-group links will be given a high weight and the original links will be given a small weight. This will cluster nodes in the same group. We then apply the layout to plotting the original graph, without the extra links. They were just to get a good layout.

G_Grouped = G
E(G_Grouped)$weight = 1

## Add edges with high weight between all nodes in the same group
for(i in unique(V(G)$Group1)) {
    GroupV = which(V(G)$Group1 == i)
    G_Grouped = add_edges(G_Grouped, combn(GroupV, 2), attr=list(weight=5))
} 

## Now create a layout based on G_Grouped
set.seed(567)
LO = layout_with_fr(G_Grouped)

## Use the layout to plot the original graph
plot(G, vertex.color=rainbow(3, alpha=0.4)[V(G)$Group1], layout=LO)

Graph with nodes clustered by group

If you want to go beyond this to have multiple levels of grouping, just add additional links with appropriate weights to connect the subgroups too.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...