0
$\begingroup$

This code:

ClearAll[wam, vLabels, sa, wag, cs, evc, cause, effect, chartData];

(* convert the matrix into a directed graph *)
wam = {{0, 0.03, 0, 0.03, 0.02, 0.04, 0.02, 0, 0.01, 0.01, 0.02, 0.03,
     0.01, 0.02, 0.02}, {0.05, 0, 0.07, 0, 0, 0, 0.03, 0.03, 0, 0.01, 
    0.06, 0.04, 0.01, 0.04, 0.04}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0.06, 
    0, 0, 0.07, 0, 0}, {0.05, 0, 0.03, 0, 0.04, 0.05, 0.03, 0.02, 0, 
    0.01, 0.02, 0.03, 0.01, 0.02, 0.03}, {0.06, 0, 0, 0, 0, 0.05, 
    0.04, 0.06, 0.03, 0.08, 0.06, 0.05, 0.03, 0.07, 0.06}, {0.01, 
    0.02, 0.07, 0.03, 0.06, 0, 0.02, 0.01, 0.06, 0.01, 0.09, 0.05, 
    0.07, 0.02, 0.03}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0.02, 0.06, 0.06, 
    0.01, 0, 0}, {0, 0.05, 0.06, 0.06, 0.06, 0.05, 0.09, 0, 0.06, 
    0.01, 0.02, 0.07, 0, 0.15, 0.02}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0.07, 0, 0, 0.04, 0, 0}, {0.01, 0.07, 0.03, 0.04, 0, 0.04, 0.05, 
    0.08, 0.06, 0, 0.03, 0.02, 0.01, 0.03, 0.09}, {0.04, 0.07, 0.05, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0.03, 0, 0}, {0, 0.01, 0.06, 0, 0.01, 
    0.07, 0.009`, 0.006`, 0.007`, 0.012`, 0.008`, 0, 0, 0.011`, 
    0.01`}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0.05, 0.05, 0.01, 0, 0.06, 
    0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0,
     0, 0, 0, 0.05, 0, 0, 0, 0, 0, 0}};
vLabels = {1->AGF, 2->CO12, 3->MA1, 4->MA2, 5->EGW, 6->CST, 7->WHS, 8->HOT, 9->TSC, 10->FIN, 11->EST, 12->ADM, 13->EDU, 14->HLT, 15->ENT};
vnames = {"AGF","CO12","MA1","MA2","EGW","CST","WHS","HOT","TSC","FIN","EST","ADM","EDU","HLT","ENT"};
sa = SparseArray[wam];
wag = Graph[sa["NonzeroPositions"], EdgeWeight -> sa["NonzeroValues"],
    DirectedEdges -> True, EdgeLabels -> "EdgeWeight"];

(* compute some statistics *)
evc = EigenvectorCentrality[wag] /. vLabels;
cs = FindGraphCommunities[wag] /. vLabels;
nghEdges = EdgeList[ NeighborhoodGraph[wag, 4]] /. vLabels;

(* Bubble chart *)
cause = Total[wam, {2}];
effect = Total@wam;
chartData = Transpose@{cause, effect, evc};

BubbleChart[
 <|Thread[vnames -> chartData]|>,
 ChartLabels -> Automatic,
 Frame -> True,
 FrameLabel -> {Style["Cause", Bold], Style["Effect", Bold]},
 RotateLabel -> True,
 ImageSize -> 300
 ]

produces:

enter image description here

I want to further extend this bubble chart by adding 4 pieces of information:

  1. Assign the same color for community members in each community;
  2. Show directed edges from a given vertex V1 by using NeighborhoodGraph[g,V1];
  3. The final Manipulate[.] code should allow for a selection of NeighborhoodGraph[.] vertex V1 using ControlType -> Setter for vertices.
  4. Finally, PlotLabels should be added to the bubble chart to identify each community (with different colors) and the definition of the directed linkages from/to the selected vertex V1.

Manipulate[...] be of the form so that once an input matrix is given, the bubble chart described above should be generated.

The above code shows what I could do so far.

EDIT

With the code by @Bob Hanlon, the bubble chart is generated by:

BubbleChart[
 Association /@ 
  GatherBy[Normal[<|Thread[vnames -> chartData]|>], 
   Position[cs, ToExpression[#[[1]]]][[1, 1]] &],
 ChartLabels -> Automatic,
 ChartStyle -> "Pastel",
 ChartLegends->Placed[{"comm 1","comm 2","comm 3","comm 4"}, Below],
 FrameLabel -> (Style[#, Bold] & /@ {"Cause", "Effect"}),
 PlotTheme -> "Business",
 PlotLabel->"Sector dominance, centrality, communities and neighborhood graph",    
 ImagePadding->{{Automatic, Automatic}, {Automatic, Scaled[.01]}},
 ImageSize -> 375
 ]

enter image description here

I still need to map NeighborhoodGraph[wag, 4], for example, on top of the bubble chart by keeping the coordinates of vertices unchanged. Experiments with each vertex should be done by Manipulate[...].

$\endgroup$
7
  • 1
    $\begingroup$ The code does not produce the shown chart. vnames is not defined. $\endgroup$
    – Bob Hanlon
    Commented Aug 10, 2023 at 16:22
  • $\begingroup$ @Bob Hanlon: Sorry for my mistake. Please see the edition in the question. $\endgroup$ Commented Aug 10, 2023 at 16:27
  • 1
    $\begingroup$ To color the bubbles use BubbleChart[Association /@ GatherBy[Normal[<|Thread[vnames -> chartData]|>], Position[cs, ToExpression[#[[1]]]][[1, 1]] &], ChartLabels -> Automatic, FrameLabel -> (Style[#, Bold] & /@ {"Cause", "Effect"}), ImageSize -> 300] I don't understand the rest of the question. $\endgroup$
    – Bob Hanlon
    Commented Aug 10, 2023 at 17:15
  • $\begingroup$ @Bob Hanlon: The rest of the question is all about plotting the edges in a NeighborhoodGraph[wag, 4] on to the bubble chart by keeping the positions of vertices in the bubble chart fixed. Using Manipulate would be preferable to repeat the same plotting for each one of the vertices in the bubble chart. $\endgroup$ Commented Aug 10, 2023 at 17:27
  • 1
    $\begingroup$ @TugrulTemel NeighborhoodGraph[wag, 4] gave almost full graph since graph is almost complete graph. Do you want to just edge coming to vertex or out? $\endgroup$
    – halmir
    Commented Aug 11, 2023 at 16:45

1 Answer 1

2
$\begingroup$

Construct a weighted graph:

wag = WeightedAdjacencyGraph[vnames, wam, 0, 
  EdgeLabels -> 
   Placed["EdgeWeight", Automatic, NumberForm[#, {Infinity, 2}] &]];

evc = EigenvectorCentrality[wag];
cs = FindGraphCommunities[wag];

I modified evc to show 0 size vertex in BubbleChart:

cause = Total[wam, {2}];
effect = Total@wam;
chartData = Transpose@{cause, effect, evc /. {0. -> .001}};

bubble = 
 BubbleChart[
  Association /@ 
    Map[Style[#[[1]], Bold, 
        Rescale[#[[2, 3]], 
         MinMax[chartData[[All, 3]]], {7, 15}] ] -> #[[2]] &, 
     GatherBy[Normal[<|Thread[vnames -> chartData]|>], 
      Position[cs, #[[1]]][[1, 1]] &], {2}], 
  ChartLabels -> Callout[Automatic], ChartStyle -> "Pastel", 
  ChartLegends -> 
   Placed[{"comm 1", "comm 2", "comm 3", "comm 4"}, Below], 
  FrameLabel -> (Style[#, Bold] & /@ {"Cause", "Effect"}), 
  PlotTheme -> "Business", 
  PlotLabel -> 
   "Sector dominance, centrality, communities and in-out edges", 
  ImagePadding -> {{Automatic, Automatic}, {Automatic, Scaled[.01]}}, 
  ImageSize -> 375]

Extract disks inside the chart to use for vertex sizes in a graph:

disk = Cases[bubble, _Disk, Infinity];
vsize = Rule @@@ (disk /. 
     Thread[Sort[disk[[All, 1]]] -> 
       VertexList[wag][[Ordering[chartData[[All, ;; 2]]]]]]);

Create a new graph with new options:

ngraph = 
  Graph[wag, VertexCoordinates -> chartData[[All, ;; 2]], 
   VertexSize -> vsize, VertexShapeFunction -> None, 
   EdgeStyle -> 
    Directive[Thickness[.01], Arrowheads[.04], GrayLevel[.5, .5]], 
   PerformanceGoal -> "Quality"];

Final manipulator:

Manipulate[
 Block[{es}, 
  es = Switch[edges, "Out", 
    If[# =!= {}, Style[#, Blue], #] &@
     EdgeList[ngraph, vertex \[DirectedEdge] _], "In", 
    If[# =!= {}, Style[#, Pink], #] &@
     EdgeList[ngraph, _ \[DirectedEdge] vertex], "Both", 
    List[If[# =!= {}, Style[#, Blue], #] &@
      EdgeList[ngraph, vertex \[DirectedEdge] _], 
     If[# =!= {}, Style[#, Pink], #] &@
      EdgeList[ngraph, _ \[DirectedEdge] vertex]], _, {}];
  Show[{bubble, 
    If[es === None || Length[es] == 0, {}, 
     HighlightGraph[ngraph, es, 
      GraphHighlightStyle -> "DehighlightHide"]]}]], {edges, {None, 
   "In", "Out", "Both"}}, {vertex, VertexList[ngraph]}, 
 SaveDefinitions -> True]

enter image description here

$\endgroup$
16
  • $\begingroup$ This is excellent!!! It is very appealing, especially the presentation of the arrows. How can I adjust the size of vertex names on the graph? They look disproportional. Many thanks for your effort. $\endgroup$ Commented Aug 11, 2023 at 20:41
  • $\begingroup$ Can you also add "Both" as a control to see both in and out links of a chosen sector? This will complete the full picture of the sector degree. $\endgroup$ Commented Aug 11, 2023 at 21:45
  • 1
    $\begingroup$ @TugrulTemel edited $\endgroup$
    – halmir
    Commented Aug 11, 2023 at 23:38
  • 1
    $\begingroup$ @TugrulTemel updated $\endgroup$
    – halmir
    Commented Aug 12, 2023 at 14:09
  • 1
    $\begingroup$ @TugrulTemel that's for rescaling text size. You can modify whatever value you want. $\endgroup$
    – halmir
    Commented Aug 28, 2023 at 14:54

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.