# How to add a legend to a graph to depict vertex properties?

I have made an animated graph that shows the changes of certain functions in time with different vertex sizes and colors:

gr = TreeGraph[RandomInteger[#] <-> # + 1 & /@ Range[0, 30],
VertexSize -> 0];

Manipulate[
Show[{SetProperty[
gr, {VertexSize ->
Table[ii -> 0.4 Sin[ii tstep]^2, {ii, VertexList[gr]}],
VertexStyle ->
Table[ii -> ColorData["Rainbow"][Cos[ii tstep]^2], {ii,
VertexList[gr]}]}],
Graphics[
Text[Style[StringJoin["T=" , ToString[tstep]], Bold, 20],
Scaled[{0, 1}]], ImageSize -> {250, 100}]}], {tstep, 0, 5, 0.1}]


A sample result looks like this: How can I add a legend to show the scale of vertex sizes and colors? The closest solution I found was this one but I don't know how to implement the scaling part for what's shown in the legend to follow the style of the graph nodes.

You can also use GraphComputationGraphPropertyChart as a legend.

Using @halmir's scaling parameters:

gr = TreeGraph[RandomInteger[#] <-> (# + 1) & /@ Range[0, 30],
VertexSize -> 0, PerformanceGoal -> "Speed"];

Manipulate[vsize = Table[ii -> 0.4 Sin[ii tstep]^2, {ii, VertexList[gr]}];
vstyle = Table[ii -> ColorData["Rainbow"][Cos[ii tstep]^2], {ii, VertexList[gr]}];
vsizescaled = Rescale[vsize[[All, 2]], Through[{Min, Max}[vsize[[All, 2]]]], {.5, 12}];
Labeled[Legended[Show @ SetProperty[gr, {VertexSize -> vsize, VertexStyle -> vstyle,
ImageSize -> 400}],
GraphComputationGraphPropertyChart[gr, Automatic -> vsizescaled,
ChartStyle -> vstyle[[All, 2]], ImageSize -> 400]],
Style[StringJoin["T = ", ToString[tstep]], Bold, 20, "Panel"], Top],
{{tstep, 2}, 0, 5, 0.1} Using a BarChart as the legend

BarChart[vsizescaled, ChartStyle -> vstyle[[All, 2]], BarSpacing -> 0,
AspectRatio -> 4, BarOrigin -> Left, Axes -> False, PerformanceGoal -> "Speed",
ChartLabels -> Placed[VertexList[gr], Before], ImageSize -> 100]


we get You can also use slightly modified version of the legend in @halmir's answer as ChartLegends in GraphComputationGraphPropertyChart

ChartLegends -> PointLegend[Directive @@@ Transpose[{vstyle[[All, 2]],
AbsolutePointSize /@ vsizescaled}],  VertexList[gr],
LegendMarkerSize -> 20, LegendLayout -> {"Column", 4}]


to get • this is so cool, +1. – Alucard Dec 25 '17 at 4:14
• Amazing! Didn't even think of having a BarChart as the legend. – MathX Dec 28 '17 at 20:20
• @MathX, thank you for the accept. – kglr Dec 29 '17 at 4:41

I think, this could be a starting point:

gr = TreeGraph[RandomInteger[#] <-> # + 1 & /@ Range[0, 30],
VertexSize -> 0];

Manipulate[
GraphicsRow[{Show[{SetProperty[
gr, {VertexSize ->
Table[ii -> 0.4 Sin[ii tstep]^2, {ii, VertexList[gr]}],
VertexStyle ->
Table[ii -> ColorData["Rainbow"][Cos[ii tstep]^2], {ii,
VertexList[gr]}]}],
Graphics[
Text[Style[StringJoin["T=", ToString[tstep]], Bold, 20],
Scaled[{0, 1}]]]}],
Graphics[{PointSize[0.05], #[], Point[#[]],
Text[ToString[#[]], #[]]} & /@
Transpose@{Table[0.4 Sin[ii tstep]^2, {ii, VertexList[gr]}],
Table[ColorData["Rainbow"][Cos[ii tstep]^2], {ii,
VertexList[gr]}],
Tuples[{Rescale[Range], Rescale[Range]}],
Tuples[{Rescale[Range, {1, 4}, {0.15, 1.15}],
Rescale[Range]}]}]
}, ImageSize -> {900, 350}
],
{tstep, 0, 5, 0.1}
] • I like it. Thanks. It gave me some ideas but let's see if others can come up with any alternatives. I was thinking of having the range of sizes and range of colors depicted in several classes. These are scaled based on the total range of the changes and won't change from frame to frame. – MathX Nov 30 '17 at 21:30
• You are welcome. Yes, let see what others add or propose. Indeed, there are masters around here, :)) – José Antonio Díaz Navas Nov 30 '17 at 21:36

Using PointLegend:

Manipulate[
vsize = Table[ii -> 0.4 Sin[ii tstep]^2, {ii, VertexList[gr]}];
vstyle =
Table[ii -> ColorData["Rainbow"][Cos[ii tstep]^2], {ii,
VertexList[gr]}];
Legended[Show[{SetProperty[
gr, {VertexSize -> vsize, VertexStyle -> vstyle}],
Graphics[
Text[Style[StringJoin["T=", ToString[tstep]], Bold, 20],
Scaled[{0, 1}]]]}],
PointLegend[
Directive @@@
Transpose[{vstyle[[All, 2]],
AbsolutePointSize /@
Rescale[vsize[[All, 2]], MinMax[vsize[[All, 2]]], {.5, 12}]}],
VertexList[gr], LegendFunction -> "Panel"]], {tstep, 0, 5, 0.1}]
`
• This is great. Thanks! I think I can work on this to get the static legend that I want. – MathX Dec 1 '17 at 18:56