4
$\begingroup$

How can I extract the colors of the individual vertices/communities from a community graph plot? E.g.

g = RandomGraph[{20, 40}];
CommunityGraphPlot[g]

CommunityGraphPlot of a random graph

I would like to plot each vertex number with its respective community-graph plot-color in another plot. So far, I was able to get a list of the vertices in the respective communities with FindGraphCommunities[g]. How can I continue from here?

Solution based on Szabolcs answer:

By employing

foundCommunities = FindGraphCommunities[g]
communityColors = GraphComputation`GraphInformationDump`$AutomaticColorList
Table[Thread[foundCommunities[[i]] -> communityColors[[i]]], {i, 1,Length@foundCommunities}]

I get

{1->Hue[0, 1, 0.8], 5->Hue[0, 1, 0.8], ...}, {10->Hue[0.14, 1, 0.9],...},...}

which I can use in a different plot.

$\endgroup$
  • 1
    $\begingroup$ Can you explain the use case (within the question itself)? Do you need to use these colours in a legend? $\endgroup$ – Szabolcs May 15 '17 at 12:20
  • $\begingroup$ I am wondering what you are using this function for. $\endgroup$ – Szabolcs May 15 '17 at 12:37
  • $\begingroup$ I want to use the colors associated with the individual vertex numbers in a different plot. $\endgroup$ – M. Heuer May 15 '17 at 13:11
4
$\begingroup$

Edited based on @halmir's comment.

You can specify the colours manually by wrapping the community specifications in Style[..., colour].

For example,

g = RandomGraph[{10, 20}]

communities = FindGraphCommunities[g]
(* {{4, 6, 7, 8, 10}, {1, 2, 9}, {3, 5}} *)

CommunityGraphPlot[g, 
 MapThread[Style, {communities, ColorData[81] /@ Range@Length[communities]}]]

Mathematica graphics

Let us also evaluate the MapThread[...] separately to see what the community specification should look like.

MapThread[Style, {communities, 
  ColorData[81] /@ Range@Length[communities]}]

Mathematica graphics

Or in InputForm:

{Style[{4, 6, 7, 8, 10}, Hue[0.58, 1, 0.5]], 
 Style[{1, 2, 9}, Hue[0.12, 1, 0.9]], 
 Style[{3, 5}, Hue[0, 1, 0.75]]}

Where do the default colours come from?

CommunityGraphPlot uses HighlightGraph under the hood to highlight the vertices. HighlightGraph uses the following internal colour function:

GraphComputation`GraphInformation["DefaultGroupColorFunction"]

This colour function takes the colours periodically from the following list:

GraphComputation`GraphInformationDump`$AutomaticColorList

For reference, in version 11.1.1 that is

{Hue[0, 1, 0.8], Hue[0.14, 1, 0.9], Hue[0.8, 0.6, 0.8], Hue[0.07, 1, 1], 
 Hue[0.2, 1, 0.6], Hue[0.1, 0.6, 0.7], Hue[0.5, 1, 0.7]}

If you want to re-use the colours in another place (for example add a legend), the cleanest solution is to specify the colours manually, as in the first example I showed.

$\endgroup$
  • $\begingroup$ you could specify color of community by wrapping groups with Style: CommunityGraphPlot[g, {Style[{4, 5, 7, 1, 2}, Green],Style[ {3, 6, 9, 8},Red]}] $\endgroup$ – halmir May 15 '17 at 13:59
  • $\begingroup$ @halmir Thank you. $\endgroup$ – Szabolcs May 15 '17 at 15:11

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.