# How to link a variable to vertices of a directed graph using bubles with different sizes

This Code produces a directed graph:

SeedRandom[12];
mat = RandomReal[{0, 2}, {5, 5}];
select[matrix_, lB_, uB_] := matrix*Map[Boole[lB <= # <= uB] &, matrix, {-1}];
sa = SparseArray[select[mat, .1, .4]];
weightedG = Graph[sa["NonzeroPositions"], EdgeWeight -> sa["NonzeroValues"],
DirectedEdges -> True, VertexLabels -> Automatic]


I like to attach a variable:

emp = {10, 45, 2, 1, 49};


to each vertex using bubbles with different sizes based on the variable emp, that is, the bubble size of vertex 1 should be a normalized size (i.e., 10/107), for vertex 2 (45/107), and so on. Elements of the variable emp are associated with five vertices {1,2,3,4,5}, respectively.

The final directed graph should be one with vertices of different bubble sizes.

• Maybe with VertexShapeFunction -> "Circle", VertexSize -> Thread[Rule[Range[5], emp/Total[emp]]]' ? Sep 29, 2021 at 16:26
• @b.gates.you.know.what: It worked, thank you very much. I will accept it if you give an answer. Sep 29, 2021 at 17:33
• @b.gates.you.know.what: When I change the range of select[...], then the number of linkages and vertices also change. This change should be carried out in the selection of elements in emp. At present, the code works only for the digraph with 5 vertices. Sep 29, 2021 at 17:46

You can use emp as the setting for VertexSize:

Graph[sa["NonzeroPositions"],
VertexSize -> {v_ :> ({#, #} & @ Normalize[emp, Total][[v]])},
PerformanceGoal -> "Quality", (* so that arrow heads are not covered by vertex disks *)
ImagePadding -> 10, (* so that vertex labels are not clipped *)
EdgeWeight -> sa["NonzeroValues"],
DirectedEdges -> True,
VertexLabels -> Automatic]


• It works fine but if you try emp = {10, 45, 80, 1, 30, 90, 25, 20, 17, 39}; sa = SparseArray[select[mat, .1, .2]] I get an uncomfortable graph in which vertices are mixed up.! Sep 29, 2021 at 21:54
• also use this: mat = RandomReal[{0, 2}, {10, 10}]; Sep 29, 2021 at 21:58
• @TugrulTemel, I think we need to set VertexCoordinates manually or use a layout like GraphLayout -> "CircularEmbedding" to avoid vertex overlaps. You can also try VertexSize -> {v_ :> {"Nearest", Rescale[emp, MinMax@emp, {.5, .9}][[v]]}}.
– kglr
Sep 30, 2021 at 13:44

emp = {10, 45, 2, 1, 49};
`