4
$\begingroup$

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.

$\endgroup$
3
  • 1
    $\begingroup$ Maybe with `VertexShapeFunction -> "Circle", VertexSize -> Thread[Rule[Range[5], emp/Total[emp]]]' ? $\endgroup$ Sep 29, 2021 at 16:26
  • $\begingroup$ @b.gates.you.know.what: It worked, thank you very much. I will accept it if you give an answer. $\endgroup$ Sep 29, 2021 at 17:33
  • $\begingroup$ @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. $\endgroup$ Sep 29, 2021 at 17:46

2 Answers 2

3
$\begingroup$

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]

enter image description here

$\endgroup$
3
  • $\begingroup$ 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.! $\endgroup$ Sep 29, 2021 at 21:54
  • $\begingroup$ also use this: mat = RandomReal[{0, 2}, {10, 10}]; $\endgroup$ Sep 29, 2021 at 21:58
  • 1
    $\begingroup$ @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]]}}. $\endgroup$
    – kglr
    Sep 30, 2021 at 13:44
4
$\begingroup$

With your definitions:

emp = {10, 45, 2, 1, 49};
normemp = Thread[Range[Length[#]] -> Normalize[#, Total]] &@emp;

ClearAll[f]
f[{x_, y_}, v_, {w_, h_}] := Disk[{x, y}, v /. normemp]

Graph[
 sa["NonzeroPositions"], EdgeWeight -> sa["NonzeroValues"], 
 DirectedEdges -> True, VertexLabels -> Automatic, 
 VertexShapeFunction -> f
]

graph with circle labels with proportional sizes

$\endgroup$

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.