4
$\begingroup$

Suppose we have the following random graph

SeedRandom[100]
n = 15;
m = 20;
G = RandomGraph[{n, m}, VertexLabels -> "Name"];

enter image description here

and a list of weights for the nodes (which is a function of their connectivity):

ls = {0.182869, 0.403493, 0.268327, 0.052163, 0.253522, 0.240516, \
0.524532, 0.135177,0., 0.208672, 0.275441, 0., 0., 0.282883, 0.246786}
  • Is there a way we could include the node-weights in how we style and visualize the nodes in the graph? for instance, different colors or sizes assigned to the weights, knowing that individual weight values are bounded between $0$ to $1.$ Any ideas would be very helpful.
$\endgroup$
1
  • 2
    $\begingroup$ Did you see VertexSize and VertexStyle? $\endgroup$
    – Szabolcs
    Commented Oct 31, 2019 at 13:21

1 Answer 1

6
$\begingroup$

You can use VertexSize and VertexStyle (suggested by Szabolcs in comments) in two ways:

SetProperty[G, 
  {VertexSize -> Thread[VertexList[G] -> ls],
   VertexStyle -> Thread[VertexList[G] -> (ColorData["Rainbow"] /@ Rescale[ls])]}]

or

SetProperty[G, 
  {VertexSize -> {v_ :> ls[[v]]},
   VertexStyle -> {v_ :> (ColorData["Rainbow"]@Rescale[ls][[v]])}}]

enter image description here

You can rescale ls to make the vertices with weight 0 more visible

G2 = SetProperty[G, 
   {VertexSize -> {v_ :> Rescale[ls[[v]], MinMax @ ls, {.1, 1}]},
    VertexStyle -> {v_ :> (ColorData["Rainbow"] @ Rescale[ls][[v]])}}]

and add a legend with markers showing both colors and relative sizes:

legendLayout = Framed[#, FrameStyle -> None] &@
  Grid[Prepend[MapIndexed[{#2[[1]], #[[1]], Item[#[[2]],
      Alignment -> {Decimal, Center}]} &, #],
    Item[Style[#, 14], Background -> LightBlue, Alignment -> {Center, Center}] & /@ 
     {"vertex", "color", "weight"}], 
   Alignment -> {Center, Center}, 
   Dividers -> {None, {1 -> True, 2 -> True, -1 -> True}}] &;

legend = SwatchLegend[ColorData["Rainbow"] /@ Rescale[ls], ls, 
   LegendMarkers -> Thread[{"Bubble", Rescale[ls, MinMax@ls, {5, 25}]}], 
   LegendLayout -> legendLayout];

Legended[G2, legend]

enter image description here

You can also use GraphComputation`GraphPropertyChart to get a circular bar chart showing ls as vertex properties:

 GraphComputation`GraphPropertyChart[G, 
    Automatic -> Rescale[ls, MinMax @ ls, {.05, 1}], 
    ColorFunction -> "Rainbow", 
    ChartLegends -> legend]

enter image description here

Using it with a custom ChartElementFunction:

cEF[{{t0_, t1_}, {r0_, r1_}}, ___] :=  Disk[r1 {Cos[(t0 + t1)/2], Sin[(t0 + t1)/2]}, 
     Pi Abs[t1 - t0]]

GraphComputation`GraphPropertyChart[G, 
  Automatic -> Rescale[ls, MinMax@ls, {.05, 1}], 
  ColorFunction -> "Rainbow", ChartLegends -> legend,
  ChartElementFunction -> cEF, ChartLabels -> {}, 
  LabelingFunction -> (Placed[Style[#2[[2]], 16], #2[[2]] /.
    {4 | 9 | 13 -> "RadialCallout", 12 -> "RadialOutside", _ -> "RadialEdge"}] &), 
  ImagePadding -> 35, ImageSize -> 500]

enter image description here

$\endgroup$
2
  • $\begingroup$ Many many thanks! For somewhat larger graphs, which visualisation approach do you reckon is more suited? For instance in case of such graph SeedRandom[123] n = 500; m = 800; g = RandomGraph[{n, m}]; comp = ConnectedComponents[g]; g = IndexGraph@ Subgraph[g, comp[[Ordering[Length /@ comp, -1][[1]]]]]; n = VertexCount[g]; m = EdgeCount[g]; ls=RandomReal[1,n]; $\endgroup$
    – user52181
    Commented Nov 2, 2019 at 4:10
  • $\begingroup$ @user929304, the first approach is faster and more flexible in that you can play with different settings for the option GraphLayout. $\endgroup$
    – kglr
    Commented Nov 2, 2019 at 15:21

Your Answer

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