5
$\begingroup$

Is there a way to change the colours of nodes within one community? Mathematica assigns vertex colours automatically to each community. However, I'd rather like to use my own colour set.

CommunityGraphPlot[ExampleData[{"NetworkGraph", "DolphinSocialNetwork"}]]

Mathematica graphics

$\endgroup$
0

4 Answers 4

3
$\begingroup$
 g = CommunityGraphPlot[
 ExampleData[{"NetworkGraph", "DolphinSocialNetwork"}]];

One method is to find the color set ,and then build a mapping, and replace the color by your own colorset.

colorList = Cases[g, Style[x__, color__] :> color, \[Infinity]] // Union;

rules = Thread[colorList -> {Red, Green, Blue, Yellow}];

g /. rules

enter image description here

$\endgroup$
5
$\begingroup$

I'd prefer the method in @halmir's answer, but using Style wrapper on the second argument of CommunityGraphPlot does not work in Version 9.0.1.0 (Windows 8 64bit). An alternative is to use VertexStyle with some pre-processing:

ClearAll[cgpF];
cgpF[g_Graph, coms_: Automatic, 
   cols_: GraphComputation`GraphInformationDump`$AutomaticColorList, 
   opts : OptionsPattern[CommunityGraphPlot]] :=
  Module[{gc = If[coms === Automatic, FindGraphCommunities[g], coms],vs, t},
   t = Thread[{gc, PadRight[cols, Length@gc, cols]}];
   vs = Join @@ (Function[{v, c}, # -> c & /@ v] @@@ t);
   CommunityGraphPlot[g, gc, VertexStyle -> vs, opts]];

Examples:

g1 = ExampleData[{"NetworkGraph", "DolphinSocialNetwork"}];
cgpF[g1]

enter image description here

cgpF[g1, Automatic, cl = ColorData[43, "ColorList"], VertexSize -> 1,
 CommunityRegionStyle -> (Directive[EdgeForm[{Thickness[.01], Lighter[#, .4]}],
      Lighter[#, .7]] & /@ cl)]

enter image description here

randomcommunities = Internal`PartitionRagged[VertexList[g1],
   RandomChoice[IntegerPartitions[VertexCount[g1], {4}]]];
cgpF[g1, randomcommunities, cl = {Red, Cyan, Orange, Green}, VertexSize -> 1.5, 
 CommunityRegionStyle -> (Directive[{EdgeForm[{Thickness[.01], Lighter[#, .4]}],
       Opacity[.2], #}] & /@ cl)]

enter image description here

$\endgroup$
4
$\begingroup$

You could find community first and use Style wrapper over it:

comm = FindGraphCommunities[g];

color = ColorData[22, "ColorList"];

CommunityGraphPlot[g, Thread[Style[comm, color[[;; Length[comm]]]]]]

enter image description here

$\endgroup$
3
$\begingroup$

I appreciate other solutions. However, one can find that CommunityGraphPlot uses HighlightGraph to highlight communities. By default, HighlightGraph uses colors from GraphComputation`GraphInformationDump`defaultGroupColorFunction. So one can redefine it

ClearAll[GraphComputation`GraphInformationDump`defaultGroupColorFunction];
GraphComputation`GraphInformationDump`defaultGroupColorFunction[n_] :=
  ColorData[22, "ColorFunction"] /@ Range@n

CommunityGraphPlot[ExampleData[{"NetworkGraph", "DolphinSocialNetwork"}]]

enter image description here

P.S. It is totally undocumented just-for-fun solution. defaultGroupColorFunction should be redefined after first evaluation of any Graph command.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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