The igraph of R language as a graph of community excavation
Input: Network topology file (edge); Community partitioning result file (Vertex community number)
Output: A result map of community partitioning
Code:
##### #测试代码 ##### #if (T) { Library (igraph) Library (grdevices) rm (List=ls ()) network=read.csv ("./ Dataset/data/karate.txt ", sep=" ", header=f) community=read.csv ("./result/lporigin_karate/2.1_of_1 ", sep=" \ T ", HEADER=F) label=community[,2] #获取按顶点ID排序后的各顶点的标号 #将label中各顶点的社区号映射为颜色 ramp =colorramp (c ("Red", "white "," Blue ")); Ramp (SEQ (0, 1, length = length (unique label))) Panel=rgb (Ramp (SEQ (0, 1, length = length (unique label))), max = 255) #设定颜色, the number of colors equals the number of communities g = Graph.data.frame (network,directed=f,vertices=community[,1]) Map=rep (0,length ( label)) #产生一个全零向量 Color=rep ("", Length (label)) #存放每个顶点的颜色 index=1 for (i-in-unique (label)) { map[ I]=index index=index+1 } for (i in 1:length (label)) {#将每个社区的顶点颜色设置为同一颜色 color[i]=panel[map[ Label[i]] } V (g) $color =color# set vertex color plot (g,vertex.label.dist=1,vertex.label.font=3, Vertex.label.cex=1.5,vertex.label.color=1)}
./dataset/data/karate.txt file Contents: Each line represents an edge format in the network: Vertex designator vertex label
2 13 13 24 14 24 35 16 17 17 57 68 18 28 38 49 19 310 311 111 511 612 113 113 414 114 214 314 417 617 718 118 220 120 222 122 226 2426 2528 328 2428 2529 330 2430 2731 231 932 132 2532 2632 2933 333 933 1533 1633 1933 2133 2333 2433 3033 3133 3 234 934 1034 1434 1534 1634 1934 2034 2134 2334 2434 2734 2834 2934 3034 3134 3234 33
./result/lporigin_karate/2.1_of_1 file Contents: Format per line: Vertex designator Community number
2212324251761771782934102111712213214217171822022222634243425342834293430342734313432343334153416341934213423343434
Result diagram:
R language Igraph as a community excavation result map