5
$\begingroup$

Consider the graph generated by the code below. How can I customize the communities the code generates where each community contains only nodes with the same color; that is, the 4 red nodes would be one community containing just the 4 red nodes, another community would be one community containing just the green nodes, and finally another community would be one containing just the orange nodes.

Also, the code needs to create the number communities equal to the number of node colors in the basic graph. Thoughts and suggestions most appreciated ... prg

edges = {NS <-> N1, NS <-> N11, NS <-> N18, NS <-> N24, NS <-> N27, 
  NS <-> N35, NS <-> N37, N1 <-> N4, N1 <-> N5, N1 <-> N6, N1 <-> N7, 
  N1 <-> N8, N1 <-> N9, N2 <-> N5, N3 <-> N5, N3 <-> N15, N4 <-> N2, 
  N4 <-> N3, N4 <-> N5, N4 <-> N12, N4 <-> N13, N4 <-> N14, 
  N5 <-> N15, N5 <-> N16, N5 <-> N19, N6 <-> N15, N7 <-> N10, 
  N8 <-> N16, N9 <-> N10, N10 <-> N5, N10 <-> N6, N10 <-> N8, 
  N11 <-> N6, N11 <-> N8, N11 <-> N21, N12 <-> N17, N12 <-> N40, 
  N13 <-> N6, N13 <-> N21, N14 <-> NG, N15 <-> N30, N16 <-> N6, 
  N17 <-> NG, N18 <-> N15, N19 <-> N20, N20 <-> N21, N20 <-> N30, 
  N21 <-> N22, N22 <-> N23, N22 <-> N43, N23 <-> NG, N24 <-> NG, 
  N25 <-> NG, N26 <-> N29, N26 <-> N40, N27 <-> N28, N27 <-> N40, 
  N28 <-> NG, N29 <-> NG, N30 <-> N31, N30 <-> N32, N30 <-> N33, 
  N30 <-> N34, N31 <-> N36, N32 <-> N36, N33 <-> N36, N34 <-> N31, 
  N35 <-> N34, N36 <-> N12, N36 <-> N39, N37 <-> N36, N38 <-> N40, 
  N38 <-> N43, N39 <-> N40, N40 <-> N41, N41 <-> N42, N42 <-> NG, 
  N43 <-> N25, N43 <-> N26, N36 <-> N38}

g = Graph[edges, VertexLabels -> "Name", VertexLabels -> Automatic, 
  VertexSize -> 0.7, 
  VertexStyle -> {NS | N1 | NS | N11 | NS | N18 | NS | N24 | NS | 
      N27 | NS | N35 | NS | N37 | N1 | N4 | N1 | N5 | N1 | N6 | N1 | 
      N7 | N1 | N8 | N1 | N9 | N2 | N5 | N3 | N5 | N3 | N15 | N4 | 
      N2 -> Red, 
    N4 | N3 | N4 | N5 | N4 | N12 | N4 | N13 | N4 | N14 | N5 | N15 | 
      N5 | N16 | N5 | N19 | N6 | N15 | N7 | N10 | N8 | N16 | N9 | 
      N10 | N10 | N5 | N10 | N6 | N10 | N8 | N11 | N6 | N11 | N8 | 
      N11 | N21 | N12 | N17 | N12 | N40 | N13 | N6 | N13 | N21 | N14 |
       NG | N15 | N30 | N16 | N6 | N17 | NG | N18 | N15 | N19 | N20 | 
      N20 | N21 | N20 | N30 | N21 | N22 | N22 | N23 | N22 | N43 | 
      N23 | NG | N24 | NG | N25 | NG | N26 | N29 | N26 | N40 -> Green,
     N28 | NG | N29 | NG | N30 | N31 | N30 | N32 | N30 | N33 | N30 | 
      N34 | N31 | N36 | N32 | N36 | N33 | N36 | N34 | N31 | N35 | 
      N34 | N36 | N12 | N36 | N39 | N37 | N36 | N38 | N40 | N38 | 
      N43 | N39 | N40 | N40 | N41 | N41 | N42 | N42 | NG | N43 | N25 |
       N43 | N26 | N36 | N38 -> Lighter[Lighter[Orange]]}]

cg = CommunityGraphPlot[g, 
  VertexStyle -> PropertyValue[g, VertexStyle]]
$\endgroup$
5
$\begingroup$
communities = GatherBy[VertexList[g], PropertyValue[{g, #}, VertexStyle] &];

CommunityGraphPlot[g, communities, VertexStyle -> PropertyValue[g, VertexStyle]]

enter image description here

Add the option

Method -> "Hierarchical"

to get

enter image description here

$\endgroup$
  • $\begingroup$ kglr: Many thanks for your creative help; much appreciated ... prg $\endgroup$ – PRG Dec 11 '19 at 1:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.