11
$\begingroup$

I wonder about how I could make a Graph scale automatically when I vary the size of the vertices.

I would like to visualize information with a Graph. The vertices all have e.g. additional info like a weight which I would like to visualize with the vertices drawn with different sizes.

However, when I define the sizes, the graph keeps the original (as opposed to the new) layout. The graph then becomes invisible.

Graph[{1 \[UndirectedEdge] 2, 2 \[UndirectedEdge] 3,3 \[UndirectedEdge] 1, 3 \[UndirectedEdge] 4}]

shows:

Standard Graph

Now I add the "weight"

Graph[{1 \[UndirectedEdge] 2, 2 \[UndirectedEdge] 3,3 \[UndirectedEdge] 1, 3 \[UndirectedEdge] 4},VertexSize -> {1 -> 1.1, 2 -> 1.2, 3 -> 1.3, 4 -> 1.4}]

This shows:

not scaled

What I would like is that the Graph would be like

nice

Any thoughts?

This is a graph currently working on. As you can see one vertex hits his neighbour. I would expect MM to or reposition this vertex a bit further or to scale down all nodes. Of course this can be done by dividing the vertexes by a number. But this is manual work to see what looks best. I hope there is another way.

other example

$\endgroup$
2
  • 1
    $\begingroup$ How about Graph[{1 \[UndirectedEdge] 2, 2 \[UndirectedEdge] 3, 3 \[UndirectedEdge] 1, 3 \[UndirectedEdge] 4}, VertexSize -> {1 -> {"Scaled", 1.1/15}, 2 -> {"Scaled", 1.2/15}, 3 -> {"Scaled", 1.3/15}, 4 -> {"Scaled", 1.4/15}}]? $\endgroup$ Commented Jan 20, 2012 at 14:49
  • $\begingroup$ I've considered this solution but if the difference between the weights is high (eg 20 vertexes with weight 10 against 1 with 100 will draw the large vertex on top of other neighbouring vertexes. I hope somebody knows an option that might trigger the plotting of graph after considering the vertex sizes.. $\endgroup$
    – Lou
    Commented Jan 20, 2012 at 15:14

2 Answers 2

7
$\begingroup$

Does the following do what you want?

WeightedGraph[edges_, weights_, options___]:=
  Block[{maxweight=Max[#[[2]]&/@weights]},
    Graph[edges,VertexSize->((#[[1]]->0.9*#[[2]]/maxweight)&/@weights),options]]

WeightedGraph[{1 \[UndirectedEdge] 2, 2 \[UndirectedEdge] 3,
               3 \[UndirectedEdge] 1, 3 \[UndirectedEdge] 4},
   (*weights:*) {1 -> 1.1, 2 -> 1.2, 3 -> 1.3, 4 -> 1.4}]

Mathematica graphics

The second line is basically your Graph call, except that it uses WeightedGraph instead of Graph, and the weights don't have VertexSize-> in front of them.

$\endgroup$
1
  • $\begingroup$ Great this works. Uses the Max instead of the average is the thing to go for. The fixed value 0.9 scales best indeed. $\endgroup$
    – Lou
    Commented Jan 21, 2012 at 21:44
6
$\begingroup$

Your second figure indicates that you'd like each consecutive vertex to be twice the size of the previous one, yet the weights you added won't achieve that. Here's one way of doing what you want:

Graph[{1 \[UndirectedEdge] 2, 2 \[UndirectedEdge] 3, 
  3 \[UndirectedEdge] 1, 3 \[UndirectedEdge] 4}, 
 VertexSize -> {1 -> 0.1, 2 -> 0.2, 3 -> 0.3, 4 -> 0.4}]

enter image description here

Now coming to why your weights made the vertices cover the entire page, and not scale correctly, the documentation for VertexSize notes that VertexSize->s for a number s is taken to be VertexSize->{"Nearest",s}, where {"Nearest",s} is a fraction of the minimum distance between the vertex coordinates.

In this case, the minimum distance is between nodes 1 and 2, hence setting VertexSize->1 is equivalent to setting the diameter of the circle to be the same as the node distance between 1 and 2.

This is perhaps clearer from the following example:

FullGraphics@GraphicsRow@
  Table[CompleteGraph[8, VertexSize -> s, PlotLabel -> s], {s, 0.1, 1, 0.3}]

enter image description here

$\endgroup$
1
  • $\begingroup$ thx. The example is great but what I mean is perhaps not clear enough. Suppose one of the vertexes is now say 300% larger. That vertex will still overwrite the other vertexes. I added in my question a graph I'm working on. $\endgroup$
    – Lou
    Commented Jan 20, 2012 at 15:42

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.