How to colour edges within community clusters in igraph

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-08 07:33:57

问题


I have created a graph and used the propagating labels community detection algorithm to detect subgroups in the graph. I have then plotted the graph and coloured the vertices according to their group membership using rainbow colours. Here is the code I used:

g <- watts.strogatz.game(1, 100, 5, 0.05)
clp <- cluster_label_prop(g)
V(g)$community <- clp$membership
rain <- rainbow(14, alpha=.5)
V(g)$color <- rain[V(g)$community]
plot(g, vertex.size=4, vertex.label=NA)

I would now like to colour edges that lie between members of a subgroup in the same colour as that subgroup, in order to better highlight the within-group ties. I would like to leave the between-group ties grey. I cannot work out how to do it, help would be greatly appreciated.

Thanks


回答1:


The following should work:

library(igraph)
g <- watts.strogatz.game(1, 100, 5, 0.05)
clp <- cluster_label_prop(g)
V(g)$community <- clp$membership
rain <- rainbow(14, alpha=.5)
V(g)$color <- rain[V(g)$community]

E(g)$color <- apply(as.data.frame(get.edgelist(g)), 1, 
                function(x) ifelse(V(g)$community[x[1]] == V(g)$community[x[2]], 
                                   rain[V(g)$community[x[1]]], '#00000000'))
plot(g, vertex.size=4, vertex.label=NA, edge.color=E(g)$color)



来源:https://stackoverflow.com/questions/41912732/how-to-colour-edges-within-community-clusters-in-igraph

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!