# The same value of VertexSize for vertices in different graphs causes different vertices size

I generated some graphs. The smallest vertices in the 1st graph should be the same size as the smallest vertices in the second graph. The smallest vertices have the same size (VertexSize value is the same) in each graph. When I generate those graphs the size of the smallest vertices seems different:  Those 2 graphs in cdf: dropbox link .

The size of graphs is set to 'Large'. How it can be fixed? I need that to do some animation. Thanks!

• Could you post code? – Vitaliy Kaurov Feb 24 '13 at 20:04
• @Vitaliy Kaurov Thanks for your attention. Code that generates graph is a part of larger program (for my thesis) and it uses some data (reads from a file). I think it's worthless to give a long, quite complex code where most of it is not related to the problem. I just gave graphs in CDF. All data required to generate each graph can be easily obtained, i.e. FullForm. – crobartie Feb 24 '13 at 21:08

Use Scaled sizes to set size relative to displayed size:

Row[{CompleteGraph[5, VertexSize -> .5],
CompleteGraph[10, VertexSize -> .5],
CompleteGraph[5, VertexSize -> Scaled[.1]],
CompleteGraph[10, VertexSize -> Scaled[.1]]}] You likely want to force your graphs to have the same width using ImageSize->w

• Thank you! Now works like it should :) – crobartie Feb 24 '13 at 21:10
• Just a note that if you add the option VertexShapeFunction -> "RoundedSquare" to your 4 graphs, the last two graphs fail with the error "An unknown box name was sent as the BoxForm for the expression". – ConvexMartian Oct 23 '18 at 15:23

The reason you get different vertex sizes is that by default the specified vertex size is interpreted in terms of the minimum distance between vertices. This is probably done so that no vertices touch or overlap as long as you have the vertex sizes smaller than 1.

g1 = Graph[{1 -> 2, 2 -> 3}, VertexSize -> 1/3, ImageSize -> {300, 300}];
g2 = Graph[{1 -> 2, 2 -> 3, 3 -> 1}, VertexSize -> 1/3, ImageSize -> {300, 300}];
Row[{g1 // Framed, "  ", g2 // Framed}] The trick for many of these problems is to use Scaled as mentioned by ssch in his answer. This scales the object in terms of image size. Its use for VertexSize is undocumented on the VertexSize doc page, but follows logically from many of its uses in other Graphics stuff.

However, this means that you now have to find a suitable size yourself, whereas the designed scaling method really makes sense for graphs.

You could keep using scaled sizes in terms of inter-vertex distance by using the following:

sc = Min[EuclideanDistance @@@ Subsets[AbsoluteOptions[g1, VertexCoordinates][[1, 2]], {2}]]/
Min[EuclideanDistance @@@ Subsets[AbsoluteOptions[g2, VertexCoordinates][[1, 2]], {2}]]

Row[
{
g1 // Framed, "  ",
Graph[{1 -> 2, 2 -> 3, 3 -> 1}, VertexSize -> 1/3 sc,
ImageSize -> {300, 300}] // Framed
}
] • Thank you too! Good to know for future. :) – crobartie Feb 24 '13 at 21:43

ssch's answer is very helpful. However, there is another possibility to introduce a length scale (which I prefer), namely

VertexSize -> {"Scaled",.1}


as described by the documentation article VertexSize.

This determines how large a vertex is given in units, where the "overall diagonal" of the graph equals 1.

Many of the times, this is very similar to Scaled[...]. However, Scaled[...] has the "possible issue" (see documentation of Scaled) that if your graphics has an AspectRatio that does not equal 1, the nodes of a graph get stretched.

For instance, compare the two outputs of

Graph[{1, 2, 3}, {1 <-> 2, 2 <-> 3}, VertexSize -> Scaled[0.2]]
Graph[{1, 2, 3}, {1 <-> 2, 2 <-> 3}, VertexSize -> {"Scaled", 0.2}]


Sadly, if you desire graphs of different "overall diagonal" to have the same vertex size, you will still have to rely on the solution of Sjoerd C. de Vries.

For simple graphs, a very good option is to use your taylor made VertexShapeFunction. However, be sure that you also correctly constrain the PlotRegion! The following code exemplifies this approach. Note the trick to get rid of the nasty superfluous padding (white space) around a graph (as apparent in of Sjoerd C. de Vries' solution).

graphRange[graph_, \[Delta]_] :=
Block[{vCoords, minX, maxX, minY, maxY},
vCoords = (VertexCoordinates /.
AbsoluteOptions[graph, VertexCoordinates])\[Transpose];
minX = vCoords[] // Min;
maxX = vCoords[] // Max;
minY = vCoords[] // Min;
maxY = vCoords[] // Max;
{{minX - \[Delta], maxX + \[Delta]}, {minY - \[Delta],
maxY + \[Delta]}}
];
graphSize[graph_, \[Delta]_] :=
Block[{range, sizeX}, range = graphRange[graph, \[Delta]];
sizeX = range[[1, 2]] - range[[1, 1]]
];

ShowGraph[g_, scale_, vertexSize_] :=
Show[g, PlotRange -> graphRange[g, 2 vertexSize],
ImageSize -> scale * graphSize[g, 2 vertexSize]];

vertexSize = 0.2;
vShape[{xc_, yc_}, name_, {w_, h_}] := Disk[{xc, yc}, vertexSize];
g1 = Graph[{1, 2, 3}, {1 <-> 2, 2 <-> 3},
VertexShapeFunction -> vShape];
g2 = Graph[{1, 2, 3}, {1 <-> 2, 2 <-> 3, 1 <-> 3},
VertexShapeFunction -> vShape];

scale = 100;
ShowGraph[g1, scale, vertexSize]
ShowGraph[g2, scale, vertexSize]  