4
$\begingroup$

Given this:

ClearAll[vl, el, ew, g, evc, valueAdded, community, sizeCommVA];

vl = {"stel", "segw", "ma7", "str1", "str3", "str2", "swhl", "sest", 
   "min", "sfin", "scst", "ma12", "shot", "ma8", "soth", "srtl", 
   "ma15", "ma6", "str4", "sbus", "ma13"};

el = {"stel" \[DirectedEdge] "segw", "stel" \[DirectedEdge] "ma7", 
   "segw" \[DirectedEdge] "stel", "segw" \[DirectedEdge] "str1", 
   "segw" \[DirectedEdge] "str3", "segw" \[DirectedEdge] "str2", 
   "swhl" \[DirectedEdge] "stel", "swhl" \[DirectedEdge] "sest", 
   "swhl" \[DirectedEdge] "min", "swhl" \[DirectedEdge] "sfin", 
   "scst" \[DirectedEdge] "segw", "scst" \[DirectedEdge] "ma12", 
   "scst" \[DirectedEdge] "shot", "scst" \[DirectedEdge] "ma8", 
   "scst" \[DirectedEdge] "ma7", "scst" \[DirectedEdge] "soth", 
   "srtl" \[DirectedEdge] "segw", "srtl" \[DirectedEdge] "swhl", 
   "srtl" \[DirectedEdge] "ma15", "srtl" \[DirectedEdge] "ma6", 
   "str1" \[DirectedEdge] "segw", "str1" \[DirectedEdge] "str4", 
   "str1" \[DirectedEdge] "sbus", "str1" \[DirectedEdge] "str3", 
   "str1" \[DirectedEdge] "str2", "str1" \[DirectedEdge] "ma7", 
   "sest" \[DirectedEdge] "swhl", "sest" \[DirectedEdge] "srtl", 
   "ma12" \[DirectedEdge] "scst", "ma12" \[DirectedEdge] "ma15", 
   "ma12" \[DirectedEdge] "soth", "shot" \[DirectedEdge] "scst", 
   "str4" \[DirectedEdge] "scst", "str4" \[DirectedEdge] "ma12", 
   "ma15" \[DirectedEdge] "srtl", "ma15" \[DirectedEdge] "shot", 
   "ma15" \[DirectedEdge] "str4", "ma15" \[DirectedEdge] "ma8", 
   "ma15" \[DirectedEdge] "str3", "ma15" \[DirectedEdge] "str2", 
   "ma8" \[DirectedEdge] "str1", "ma8" \[DirectedEdge] "str3", 
   "min" \[DirectedEdge] "sest", "min" \[DirectedEdge] "soth", 
   "sbus" \[DirectedEdge] "sest", "sfin" \[DirectedEdge] "shot", 
   "sfin" \[DirectedEdge] "ma6", "ma6" \[DirectedEdge] "ma8", 
   "ma6" \[DirectedEdge] "min", "ma6" \[DirectedEdge] "ma13", 
   "str3" \[DirectedEdge] "sbus", "str3" \[DirectedEdge] "str2", 
   "ma13" \[DirectedEdge] "str3", "str2" \[DirectedEdge] "str3", 
   "ma7" \[DirectedEdge] "ma13", "soth" \[DirectedEdge] "ma7"};

ew = {0.01313, 0.0139, 0.0131, 0.0188, 0.01145, 0.01842, 0.01786, 
   0.01203, 0.0137, 0.0188, 0.018, 0.01561, 0.01544, 0.01767, 0.0174, 
   0.0133, 0.0184, 0.0126, 0.01724, 0.0126, 0.0139, 0.010414`, 0.0169,
    0.01498, 0.01736, 0.01832, 0.01211, 0.011911, 0.01586`, 0.01863, 
   0.01653, 0.01533, 0.0129, 0.0121, 0.01144, 0.014, 0.01109, 0.0172, 
   0.01267, 0.0171, 0.0150, 0.01192, 0.01428, 0.017406, 0.0109, 
   0.017892, 0.01338, 0.017794, 0.01095, 0.0187, 0.0155, 0.0142, 
   0.01422, 0.01125, 0.015559, 0.01788};

g = Graph[vl, el, EdgeWeight -> ew];

evc = EigenvectorCentrality[g];
SeedRandom[45];   valueAdded = 
 Thread[vl -> RandomInteger[{1, 100}, Length[vl]]];
community = FindGraphCommunities[g];
sizeCommVA = Total[community /. valueAdded, {2}];

Create a community graph:

CommunityGraphPlot[g, VertexLabels -> "Name"]

enter image description here

I want to set the size of each community area to sizeCommVA and label each community with sizeCommVA (like C1 = 234, C2=546 etc.) and set each vertex size to evc.

$\endgroup$

2 Answers 2

3
$\begingroup$
normalizedCSizes = Normalize[sizeCommVA, Total];

communityColors = 
  GraphComputation`GraphInformationDump`$AutomaticColorList[[;; Length @ community]];

vertexSizes = AssociationThread[vl, evc];

Define a function for post-processing CommunityGraphPlot output to resize community regions:

reSizeRegions[scale_ : 1] := ReplaceAll[
   {EdgeForm[ef_], dir___, fc_FilledCurve} :> 
   {EdgeForm[ef /. AbsoluteThickness[at_] :>  AbsoluteThickness[20 scale at]], 
    dir, 
    Scale[fc, {1 + #, 1 + #} & @
       First @ Cases[ef, AbsoluteThickness[at_] :> scale at, All]]}];

First, create a plot using normalizedCSizes to set the AbsoluteThickness of community boundaries:

cgp = CommunityGraphPlot[g, ImageSize -> Large, ImagePadding -> 20, 
   VertexLabels -> "Name", 
   VertexSize -> {v_ :> 10 vertexSizes[v]},
   CommunityRegionStyle -> (Opacity[.1, #] & /@ communityColors), 
   CommunityBoundaryStyle -> 
   (MapThread[{AbsoluteThickness[ #], Opacity[.5], #2} &]@
     {Rescale[normalizedCSizes], communityColors})];

Post-process cgp using reSizeRegions to set the sizes and boundary thicknesses of community regions:

Row[{cgp, reSizeRegions[] @ cgp}, Spacer[10]]

enter image description here

Play with different values of the argument scale:

Row[Labeled[Show[reSizeRegions[#]@cgp, ImageSize -> Medium], 
      Style[PromptForm["scale", #], 16], Top] & /@ {1/3, 1/2, 2/3}, 
 Spacer[10]]

enter image description here

$\endgroup$
1
  • 1
    $\begingroup$ Thank you very much...I think that CommunityRegionStyle in cap should have communityColors instead of colors. It seems that it is a typo. $\endgroup$ Oct 8, 2023 at 21:28
3
$\begingroup$

A limited, partial solution using PlotLegends

legends = (StringForm["C``\[ThinSpace]=\[ThinSpace]``", #[[1]], #[[2]]] & /@
    Transpose[{Range[4], sizeCommVA}]);

CommunityGraphPlot[g,
  VertexLabels -> "Name",
  CommunityBoundaryStyle -> ({#, Thick} & /@ {Hue[0, 1, 0.8], 
      Hue[0.14, 1, 0.9], Hue[0.8, 0.6, 0.8], Hue[0.07, 1, 1]}),
  PlotLegends -> Placed[legends, {.85, .85}],
  ImageSize -> Large]

enter image description here

$\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.