6
$\begingroup$

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: graph 1

graph 1

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!

$\endgroup$
2
  • $\begingroup$ Could you post code? $\endgroup$ Feb 24, 2013 at 20:04
  • 1
    $\begingroup$ @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. $\endgroup$
    – Bartek
    Feb 24, 2013 at 21:08

3 Answers 3

6
$\begingroup$

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]]}]

sizes

You likely want to force your graphs to have the same width using ImageSize->w

$\endgroup$
2
  • 1
    $\begingroup$ Thank you! Now works like it should :) $\endgroup$
    – Bartek
    Feb 24, 2013 at 21:10
  • $\begingroup$ 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". $\endgroup$ Oct 23, 2018 at 15:23
6
$\begingroup$

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}]

Mathematica graphics

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
  }
]

Mathematica graphics

$\endgroup$
1
  • $\begingroup$ Thank you too! Good to know for future. :) $\endgroup$
    – Bartek
    Feb 24, 2013 at 21:43
5
$\begingroup$

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}]

enter image description here enter image description here

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[[1]] // Min;
   maxX = vCoords[[1]] // Max;
   minY = vCoords[[2]] // Min;
   maxY = vCoords[[2]] // 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]

enter image description here

enter image description here

$\endgroup$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.