# General Graph Drawing

GraphPlot and GraphPlot3D calculate and plot a visually appealing 2D/3D layout of a graph. The functions are designed to work with very large graphs and handle both connected and disconnected graphs.

GraphPlot[{v_{i1}->v_{j1},v_{i2}->v_{j2},…}] | generate a plot of the graph in which vertex is connected to vertex |

GraphPlot[{{v_{i1}->v_{j1},lbl_{1}},…}] | associate labels with edges in the graph |

GraphPlot[m] | generate a plot of the graph represented by the adjacency matrix m |

GraphPlot3D[{v_{i1}->v_{j1},v_{i2}->v_{j2},…}] | generate a 3D plot of the graph in which vertex is connected to vertex |

GraphPlot3D[{{v_{i1}->v_{j1},lbl_{1}},…}] | associate labels with edges in the graph |

GraphPlot3D[m] | generate a 3D plot of the graph represented by the adjacency matrix m |

In[3]:= |

GraphPlot may produce slightly different output on different platforms, due to floating-point differences.

## Options for GraphPlot and GraphPlot3D

The following options are accepted for GraphPlot and GraphPlot3D (DirectedEdges and EdgeLabeling options are only valid for GraphPlot), in addition, options for Graphics and Graphics3D are accepted.

option name | default value | |

DirectedEdges | False | whether to show edges as directed arrows |

EdgeLabeling | True | whether to include labels given for edges |

EdgeRenderingFunction | Automatic | function to give explicit graphics for edges |

Method | Automatic | the method used to lay out the graph |

MultiedgeStyle | Automatic | how to draw multiple edges between vertices |

PlotRangePadding | Automatic | how much padding to put around the plot |

PackingMethod | Automatic | method to use for packing components |

PlotStyle | Automatic | style in which objects are drawn |

SelfLoopStyle | Automatic | how to draw edges linking a vertex to itself |

VertexCoordinateRules | Automatic | rules for explicit vertex coordinates |

VertexLabeling | Automatic | whether to show vertex names as labels |

VertexRenderingFunction | Automatic | function to give explicit graphics for vertices |

Options for GraphPlot and GraphPlot3D.

### DirectedEdges

The option DirectedEdges specifies whether to draw edges as arrows. Possible values for this option are True or False. The default value for this option is False.

### EdgeLabeling

The option EdgeLabeling specifies whether and how to display labels given for the edges. Possible values for this option are True, False, or Automatic. The default value for this option is True, which displays the supplied edge labels on the graph. With EdgeLabeling->Automatic, the labels are shown as tooltips.

_{i}->v

_{j},lbl] to specify a tooltip for an edge. Place the cursor over the edge between vertices 3 and 6, as well as over the edge label on the edge between vertices 3 and 5, to see the tooltips.

To display the supplied label for the edge in 3D, EdgeRenderingFunction needs to be used. This is described in "Edge Rendering Function".

### EdgeRenderingFunction

The option EdgeRenderingFunction specifies graphical representation of the graph edges. Possible values for this option are Automatic, None, or a function that gives a proper combination of graphics primitives and directives. With the default setting of Automatic, a dark red line is drawn for each edge. With EdgeRenderingFunction->None, edges are not drawn.

With EdgeRenderingFunction->g, each edge is rendered with the graphics primitives and directives given by the function g that can take three or more arguments, in the form , where , are the coordinates of the beginning and ending points of the edge, , are the beginning and ending vertices, and is any label specified for the edge or None. Explicit settings for EdgeRenderingFunction->g override settings for EdgeLabeling and DirectedEdges.

In[12]:= |

In[5]:= |

### Method

Algorithms to be used in GraphPlot and GraphPlot3D can be specified using the Method option, either as Method->"name" or Method->{"name",opt_{1}->val_{1},…}, where are method-specific options, described in separate sections. Method->Automatic uses the method for trees and otherwise.

Automatic | a method suitable for the problem is chosen automatically |

"CircularEmbedding" | lay out the vertices in a circle |

"HighDimensionalEmbedding" | invoke the high-dimensional embedding method, in which the graph is first laid out in a high-dimensional space based on the graph distances of the vertices to k centers; this layout is then projected to 2D or 3D space by linear combination of the high-dimensional coordinates using principal component analysis |

"RadialDrawing" | invoke the radial drawing method, which is most suitable for tree or tree-like graphs; if the graph is not a tree, a spanning tree is first constructed, and a radial drawing of the spanning tree is used to derive the drawing for the graph |

"RandomEmbedding" | lay out vertices randomly |

"SpiralEmbedding" | lay out the vertices in a spiral; in 3D, this distributes vertices uniformly on a sphere |

"SpringElectricalEmbedding" | invoke the spring-electrical embedding method, in which neighboring vertices are subject to an attractive spring force that is proportional to their physical distance, and all vertices are subject to a repulsive electrical force that is inversely proportional to their distance; the overall energy is minimized |

"SpringEmbedding" | invoke the spring embedding method, in which a vertex is subject to either attractive or repulsive force from another vertex, as though they are connected by a spring; the spring has an ideal length equal to the graph distance between the vertices; the total spring energy is minimized |

Valid values of the Method option.

In[19]:= |

Out[19]= |

In[20]:= |

Out[20]= |

In[21]:= |

Out[21]= |

In[22]:= |

Out[22]= |

In[23]:= |

Out[23]= |

In[24]:= |

Out[24]= |

*Combinatorica*Package format using coordinates that come with it.

In[25]:= |

### MultiedgeStyle

The option MultiedgeStyle specifies whether to draw multiple edges between two vertices. Possible values for MultiedgeStyle are Automatic (the default), True, False, or a positive real number. With the default setting MultiedgeStyle->Automatic, multiple edges are shown for a graph specified by a list of rules, but not shown if specified by an adjacency matrix. With MultiedgeStyle->δ, the multiedges are spread out to a scaled distance of δ.

In[28]:= |

Out[28]= |

In[29]:= |

Out[29]= |

### PackingMethod

The option PackingMethod specifies the method used for packing disconnected components. Possible values for the option are Automatic (the default), , , , , , and . With PackingMethod->"ClosestPacking", components are packed as close together as possible using a polyomino method [6], starting from the top left. With PackingMethod->"ClosestPackingCenter", components are packed starting from the center. With PackingMethod->"Layered", components are packed in layers starting from top left. With PackingMethod->"LayeredLeft" or PackingMethod->"LayeredTop", components are packed in layers starting from the top/left respectively. With PackingMethod->"NestedGrid", components are arranged in a nested grid. The typical effective default setting is PackingMethod->"Layered", and the packing starts with the components of the largest bounding box area.

In[30]:= |

In[32]:= |

Users can adjust the packing by suboptions of PackingMethod. The suboption specifies the amount of space to allow between components; possible values are Automatic (the default), or a non-negative number. The suboption , which overrides , also specifies the amount of space to allow between components. It takes a list of the form , which are the width and height of the bounding box of the components, and returns a non-negative number. Options PackingMethod->"ClosestPacking" and PackingMethod->"ClosestPackingCenter" also accept a suboption, which specifies the average number of polyominoes used to approximate each disconnected component. Possible values for the suboption are Automatic (the default, which usually sets to 100), or a positive integer. A smaller typically has the effect of not allowing smaller components to embed in between large components.

In[34]:= |

In[36]:= |

### PlotRangePadding

PlotRangePadding is a common option for graphics functions inherited by GraphPlot and GraphPlot3D.

### PlotStyle

PlotStyle is a common option for graphics functions inherited by GraphPlot and GraphPlot3D. The option PlotStyle specifies the style in which objects are drawn.

### SelfLoopStyle

The option SelfLoopStyle specifies whether and how to draw loops for vertices that are linked to themselves. Possible values of the option are Automatic (the default), True, False, or a positive real number. With SelfLoopStyle->Automatic, self-loops are shown if the graph is specified by a list of rules, but not by an adjacency matrix. With SelfLoopStyle->δ, the self-loops are drawn with a diameter of δ (relative to the average edge length).

In[40]:= |

Out[40]= |

### VertexCoordinateRules

The option VertexCoordinateRules specifies the coordinates of the vertices. Possible values are None, a list of coordinates, or a list of rules specifying the coordinates of selected or all vertices.

In[1]:= |

### VertexLabeling

The option VertexLabeling specifies whether to show vertex names as labels. Possible values for this option are True, False, Automatic (the default), and Tooltip. VertexLabeling->True shows the labels. For graphs specified by an adjacency matrix, vertex labels are taken to be successive integers , , …, , where is the size of the matrix. For graphs specified by a list of rules, labels are the expressions used in the rules. VertexLabeling->False displays each vertex as a point. VertexLabeling->Tooltip displays each vertex as a point, but gives its name in a tooltip. VertexLabeling->Automatic displays each vertex as a point, giving its name in a tooltip if the number of vertices is not too large. You can also use Tooltip[v_{k},v_{lbl}] anywhere in the list of rules to specify an alternative tooltip for a vertex .

In[45]:= |

Out[45]= |

### VertexRenderingFunction

The option VertexRenderingFunction specifies graphical representation of the graph edges. Possible values for this option are Automatic, None, or a function that gives a proper combination of graphics primitives and directives. With the default setting of Automatic, vertices are displayed as points, with their names given in tooltips.

In[49]:= |

Out[49]= |

With VertexRenderingFunction->g, each vertex is rendered with the graphics primitives given by , where is the coordinate of the vertex and is the label of the vertex. Explicit settings for VertexRenderingFunction->g override settings for VertexLabeling.

In[52]:= |

## A Common Suboption of All Methods

All graph drawing methods accept the method suboption , which specifies the desired amount of clockwise rotation in radians from the default orientation. The option takes any numeric values, or False. The default is 0.

For GraphPlot and GraphPlot3D, the default orientation is derived by an alignment step where the principal axis is found and the graph drawing is aligned with the coordinate. However if "Rotation"->False is specified, this step is skipped.

A common suboption for all methods.

## Common Suboptions of the "SpringEmbedding" and "SpringElectricalEmbedding" Methods

Both the and methods belong to the family of so-called force-directed methods. These methods work by calculating the force on each vertex, and iteratively moving the vertex along the force in an effort to minimize the overall system's energy. See [8] for algorithmic details. These two methods have the following common options.

option name | default value | |

"EnergyControl" | Automatic | how the energy function is controlled during minimization |

"InferentialDistance" | Automatic | cutoff distance beyond which the force calculation ignores inference from faraway vertices |

MaxIterations | Automatic | maximum number of iterations to be used in attempting to minimize the energy |

"RandomSeed" | Automatic | seed to use in the random generator for initial vertex placement |

"RecursionMethod" | Automatic | whether a multilevel algorithm is used to lay out the graph |

"StepControl" | Automatic | how step lengths are modified during energy minimization |

"StepLength" | Automatic | initial step length used in moving the vertices |

"Tolerance" | Automatic | tolerance used in terminating the energy minimization process |

Common suboptions for and methods.

### "EnergyControl"

The suboption specifies limitations on the total energy of the system during minimization. Possible values are Automatic (the default), , or . When the value is , a step along the force will only be accepted if the energy is lowered. When the value is , a step along the force will be accepted even if the energy is not lowered.

### "InferentialDistance"

The suboption specifies a cutoff distance beyond which the interaction between vertices is assumed to be nonexistent. Possible values are Automatic (the default) or a positive numeric value. For the method, if the graph distance between a vertex i and a vertex j is greater than the option value of , the repulsive and attractive spring force between i and j is ignored. For the method, if the Euclidean distance between a vertex i and a vertex j is greater than the option value of , the repulsive force between i and j is ignored.

In[1]:= |

In[21]:= |

Out[21]= |

### MaxIterations

The option MaxIterations specifies the maximum number of iterations to be used in attempting to minimize the energy. Possible values are Automatic (the default) or a positive integer.

### "RandomSeed"

The option specifies a seed for the random number generator that computes the initial vertex placement. Changing this option usually affects the orientation of the drawing of the graph, but it can also change the layout. Possible values are Automatic or an integer.

In[60]:= |

### "RecursionMethod"

The option specifies whether the graph layout should be produced by a recursive procedure. Possible values are Automatic (the default), , or None. In a algorithm, the graph is successively coarsened into graphs with a smaller and smaller number of vertices. The coarser graphs are laid out first, and those layouts are interpolated into the finer graphs and then further refined.

suboption name | default value | |

"Randomization" | Automatic | whether to inspect vertices in random order |

"MinSize" | Automatic | minimal number of vertices in a coarsened graph |

"CoarseningScheme" | Automatic | how graphs are coarsened |

For the option , possible values are Automatic, True, and False. For , possible values are Automatic or a positive number. For , the implemented algorithms are based on either a maximal independent vertex set, which forms the coarse vertices, or a maximal independent edge set, also called a matching. In a matching, two vertices that form an edge are merged to form a coarse graph vertex. The following are possible values for .

"MaximalIndependentVertexSet" | link vertices in the maximal independent set if their graph distance is 3 or less |

"MaximalIndependentVertexSetInjection" | |

link vertices in the maximal independent set if their graph distance is 1 or 2 | |

"MaximalIndependentVertexSetRugeStuben" | |

generate the maximal independent vertex set, giving priority to vertices with more neighbors not in the set, then link vertices in the set if their graph distance is 3 or less | |

"MaximalIndependentVertexSetRugeStubenInjection" | |

link vertices if their graph distance is 1 or 2, giving priority to vertices with more neighbors | |

"MaximalIndependentEdgeSet" | consider edges in their natural order when matching |

"MaximalIndependentEdgeSetHeavyEdge" | |

give priority to edges with higher edge weight (i.e., edges that represent a larger number of edges in the original graph) when matching | |

"MaximalIndependentEdgeSetSmallestVertexWeight" | |

give priority to matchings of vertices with neighbors that have the smallest vertex weight |

### "StepControl"

The option defines how step length is modified during energy minimization. It can be Automatic (the default), (where step length can only be decreased), (where step length can be made larger or smaller), or (where step length is strictly reduced between iterations).

### "StepLength"

The option gives the initial step length used in moving the vertices around. Possible values are Automatic (the default) or a positive real number.

### Tolerance

The option Tolerance specifies the tolerance used in terminating the energy minimization process. If the average change of coordinates of each vertex is less than the tolerance, the energy minimization process is terminated and the current coordinates are given as output. Possible values are Automatic or a positive real number.

## Method Suboptions of the "SpringElectricalEmbedding" Method

option name | value | |

"Octree" | Automatic | whether to use an octree data structure (in three dimensions) or a quadtree data structure (in two dimensions) in the calculation of repulsive force |

"RepulsiveForcePower " | -1 | how fast the repulsive force decays over distance |

### "Octree"

The option specifies whether to use an octree data structure (in three dimensions) or a quadtree data structure (in two dimensions) in the calculation of repulsive force. Possible values are Automatic (the default), True, or False. Use of an octree/quadtree data structure minimizes the complexity of computation by approximating the long-range repulsive force. However, it introduces an approximation to the force calculation. Therefore, in a few cases the result may not be as good.

### "RepulsiveForcePower"

Possible values are negative real numbers, with -1 as the default. In the spring-electrical embedding, the repulsive force between two vertices and is by default. If the value of is (with ), then the repulsive force is defined as , where is the distance between the vertices and is a constant coefficient.

A strong long-range repulsive force over long distance often has the boundary effect that vertices in the periphery are closer to each other than those in the center are. Specifying a weaker long-range repulsive force can sometimes alleviate this effect. This option can also be useful in drawing a graph so that it fills up more space. (See the "InferentialDistance" method option for details.)

## Method Suboption of "HighDimensionalEmbedding"

option name | default value | |

"RefinementMethod" | None | whether the result should be further refined, and which method should be used for refinement |

### "RefinementMethod"

The option specifies whether the result should be further refined, and which method should be used to refine it. Possible values are None (the default), , or .

In[65]:= |

## Advanced Topics

### Drawing a Graph to Fill Up More Space

While the default setting for GraphPlot works well in general, for graphs that have a wide range of values for the vertex degree, it is often necessary to use a setting that helps the vertices to occupy less space.

In[18]:= |

### Improving Performance for Drawing Very Large Graphs

Although the default option set usually ensures a very good performance, it is often possible to further increase drawing speed and reduce memory usage by selecting specific option combinations for a particular task. For example, speed and/or memory usage can be improved using a smaller number of iterations, a smaller inferential distance, or a lower tolerance. These settings tend to reduce quality, but still frequently offer an acceptable compromise.

In[75]:= |

### Extracting Vertex Coordinates from Output

In most cases, you will deal with the output of GraphPlot and GraphPlot3D just as with a usual Graphics expression. However, you may sometimes want to take advantage of the additional information encapsulated in the output expression, which has the form Graphics[Annotation[data,VertexCoordinateRules->rules]]. Particularly, it is sometimes useful to extract coordinates of graph vertices.

## Example Gallery

*E. coli* Transcription Networks

In a graph representation of a transcriptional regulation network that controls gene expression in cells, nodes (vertices) are operons, which are one or more genes transcribed on the same messenger ribonucleic acid (mRNA). Edges of the graph are directed from an operon that encodes a transcription factor to an operon that it directly regulates [1].

#### Data

This is the network [2] described as rules.

In[86]:= |

#### Drawing the Network

### Protein: An Oxidoreductase

### Square Dielectric Waveguide

A square sparse matrix can be viewed as an adjacency matrix of a graph; therefore it is often instructive to "draw" the sparse matrix using GraphPlot. An example is given below, and the graph drawing of over one thousand matrices can be found at [7].

### Social Network

Graph drawing is a powerful tool in visualizing social structures.

### Graphs from Words and Texts

In[95]:= |

In[96]:= |

In[1]:= |

In[100]:= |

### Torus

In[102]:= |

## References

[1] Milo, R., S. Shen-Orr, S. Itzkovitz, N. Kashtan, D. Chklovskii, and U. Alon. "Network Motifs: Simple Building Blocks of Complex Networks." *Science* 298, no. 5594 (2002): 824–827.

[2] Alon, U. "Collection of Complex Networks." *Uri Alon Homepage *2007. http://www.weizmann.ac.il/mcb/UriAlon/groupNetworksData.html

[3] Milo, R., S. Itzkovitz, N. Kashtan, et al. "Superfamilies of Designed and Evolved Networks." *Science* 303, no. 5663 (2004): 1538–1542.

[4] Alon, U. "1AORInter." *network Motifs *2007. http://www.weizmann.ac.il/mcb/UriAlon/Papers/networkMotifs/1AORInter_st.txt

[5] National Institute of Standards and Technology. "DWA512: Square Dielectric Waveguide." *Matrix Market *2007. http://math.nist.gov/MatrixMarket/data/NEP/dwave/dwa512.html

[6] Freivalds, K., U. Dogrusoz, and P. Kikusts, "Disconnected Graph Layout and the Polyomino Packing Approach." *Lecture Notes in Computer Science: Revised Papers from the 9th International Symposium on Graph Drawing* 2265 (2001): 378–391.

[7] Hu, Y. F. "Graph Drawing of Square Matrices from University of Florida Sparse Matrix Collection." (2007).

[8] Hu, Y. F. "Efficient, High-Quality Force-Directed Graph Drawing." *The Mathematica Journal* 10, no. 1 (2006): 37–71.