4
$\begingroup$

Below is a graph I've colored with 3 colors Orange, Red, and Green. I would like to generate the community graph plot that uses the colors I've assigned to these nodes and not the default colors the community graph defaults to ... is there a way to control the colors to those I've assigned to the vertices?

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 -> 1, 
  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]]}]
$\endgroup$
3
  • 1
    $\begingroup$ You assigned three colors, but CommunityGraphPlot finds four communities. How should the fourth one be colored? Alternatively, you could provide a list of lists of vertices as the second argument to CommunityGraphPlot to specify the communities according to your colors. $\endgroup$
    – MarcoB
    Nov 22, 2019 at 18:21
  • $\begingroup$ I want the color of a node in any of the communities to be the color I gave it in the above code; hence, even if there are 5, 6, or 7 communities found the nodes in that community are one of the three colors given to the vertex in the above code $\endgroup$
    – PRG
    Nov 22, 2019 at 18:28
  • $\begingroup$ Ah, got it! I had misunderstood what you were after. $\endgroup$
    – MarcoB
    Nov 22, 2019 at 18:38

2 Answers 2

4
$\begingroup$

You can also use

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

enter image description here

$\endgroup$
0
$\begingroup$
> CommunityGraphPlot[g,   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]]}]
$\endgroup$

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.