# Ideas for styling graph nodes with weights

Suppose we have the following random graph

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


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.
• Did you see VertexSize and VertexStyle? Commented Oct 31, 2019 at 13:21

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

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


or

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


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]


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

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


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]]

GraphComputationGraphPropertyChart[G,
Automatic -> Rescale[ls, MinMax@ls, {.05, 1}],
ColorFunction -> "Rainbow", ChartLegends -> legend,
ChartElementFunction -> cEF, ChartLabels -> {},
LabelingFunction -> (Placed[Style[#2[[2]], 16], #2[[2]] /.

• 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];
• @user929304, the first approach is faster and more flexible in that you can play with different settings for the option GraphLayout`.