|Network Analysis with igraph|
The igraph library and the igraph R package do not introduce a new file format to store graphs. Instead if recommends using the GraphML format for smaller graphs and some simple edge list format for larger graphs, perhaps also complessed.
igraph provides the
write.graph() functions for reading and writing
graphs from and to files. Both of these functions can handle a number
of different file formats, which can be selected by the
Note however that you don't neccessarily need special igraph
functions to read and write graphs in various formats, as there is a
wealth of R functions for importing and exporting data in a variety of
formats. For example you can use the standard R
scan() to read an edge list from a file
graph() to create a graph object from
it. Similarly you can use
create an edge list from a graph object and the standard
write() to export it to a file.
As R can also import Excel files via the
file format, Excel files can also easily converted to igraph graph
objects; just like nearly any other format.
Another possiblity to store graph data in files is to use the
as provided by R. Graph objects can be safely saved and loaded this
Simple formats can tipically store the structure of a graph but no or just limited amount of meta-data like edge weights, vertex colors, etc.
Perhaps the simplest file format igraph can handle is the edge
list format. This is nothing more than a simple text file with two
whitespace separated columns containing vertex ids (ie. non-negative
So if the
graph.txt contains the following
0 1 2 3 3 2 1 0
then it can be read by setting the
Vertices: 4 Edges: 4 Directed: TRUE Edges:  0 -> 1  2 -> 3  3 -> 2  1 -> 0
read.graph() has an optional
directed argument, set this to
FALSE to create an undirected graph, the
default is directed. There is also an optional
n argument, this is the number of vertices in
the graph. Set this to the desired value if it is bigger than the
highest vertex id in the edge list file. (If it is set to a smaller
value then it will be ignored.)
These two formats were introduced by the Large Graph Layout program, they are abre to store edge weights and symbolic vertex names. See also the LGL homepage.
ncol format is simply a weighted edge list
with symbolic vertex names. The vertex names and the weight are
separaeted by white space, the vertex names themselves cannot contain
white space. The weights are optional. The vertex names are stored in
the vertex attribute
name, the edge weights are
stored in the edge attribute
weight. You may set
optional arguments to
FALSE if you don't want to
set these attributes.
If the file
the following graph:
foo bar 1 bar baz -1 foobar foo foo baz 2
then we get the following graph object:
>g <- read.graph("graph.ncol", format="ncol")
Vertices: 4 Edges: 4 Directed: FALSE Edges:  0 -- 1  1 -- 2  0 -- 3  0 -- 2
 "foo" "bar" "baz" "foobar"
 1 -1 0 2
The other LGL format is the
format. This is adjacency list like format, it might contain symbolic
vertex names and optionally edge weights:
# vertex1name vertex2name [optionalWeight] vertex3name [optionalWeight]
This is similar to an adjacency list format, the neighboring vertices for each vertex are listed. The name of the initial vertex is preceded by “#”, then in the following lines the names of its neighbors are listed optionally with the weights of the edges.
The LGL software works with undirected graphs containing no multiple
edges, this means that if vertex “B” is listed as a
neighbor of vertex “A” then vertex “A” must
not be listed as a neighbor of vertex “B”. igraph of
course is happy with multiple edges and is able to read and write
lgl files which break the original LGL software.
Pajek it a popular network analysis program for Windows. (See the Pajek hompage.) It has a quite flexible but not very well documented file format, see the Pajek manual on the Pajek homepage for some information about the file format.
igraph can read and write Pajek files, with some limitations:
.net files are supported, Pajek project
files (which can contain many graph and also other type of data)
are not. Poject files might be supported in a forthcoming igraph
release if they turned out to be needed.
Time events networks are not supported.
Hypergraphs (graphs with non-binary edges) are not supported as igraph cannot handle them.
Graphs containing both directed and undirected edges are not supported as igraph cannot represent them.
Bipartite (also called affiliation) networks are not supported. The surrent igraph version imports the network structure correctly but vertex type information is omitted.
Graph with multiple edge sets are not supported
igraph also reads the non-structural information from Pajek files, like edge weights and vertex colors and assign these as vertex and edge attributes. Note however that the names of the attributes are not always the same as in the Pajek file, some of them are renamed to be more informative, or other reasons. The following table contains the vertex attributes created by igraph:
|igraph attribute name||description, Pajek attribute|
|x, y, z||The “x”, “y” and “z” coordinate of the vertex|
|vertexsize||The size of the vertex when plotted
|shape||The shape of the vertex when plotted.|
|color||Vertex color (
|color-red, color-green, color-blue||Vertex color (
|framecolor||Border color (
|framecolor-red, framecolor-green, framecolor-blue||Border color (
|labelcolor||Label color (
|labelcolor-red, labelcolor-green, labelcolor-blue||Label color (
|labeldist||The distance of the label from the
|framewidth||The width of the border (
|fontsize||Size of the label font (
|rotation||The rotation of the vertex (
|radius||Radius, for some vertex shapes (
|diamondratio||For the diamond shape (
These igraph attributes are only created if there is at least one
vertex in the Pajek file which has the corresponding associated
information. Eg. if there are vertex coordinates for at least one
vertex then the “x”, “y” and possibly also
“z” vertex attributes will be created. For those vertices
for which the attribute is not defined,
The following edge attributes might be created:
|igraph attribute name||description, Pajek attribute|
|color||Edge color, if the color is given with a symbolic name,
|color-red, color-green, color-blue||Edge color if it was given in RGB notation,
Note vertices are numbered starting with one in Pajek, but igraph numbering starts with zero.
Here is an example Pajek file, it is part of the Pajek distribution
under the name
*Network TRALALA *vertices 4 1 "1" 0.0938 0.0896 ellipse x_fact 1 y_fact 1 2 "2" 0.8188 0.2458 ellipse x_fact 1 y_fact 1 3 "3" 0.3688 0.7792 ellipse x_fact 1 4 "4" 0.9583 0.8563 ellipse x_fact 1 *arcs 1 1 1 h2 0 w 3 c Blue s 3 a1 -130 k1 0.6 a2 -130 k2 0.6 ap 0.5 l "Bezier loop" lc BlueViolet fos 20 lr 58 lp 0.3 la 360 2 1 1 h2 0 a1 120 k1 1.3 a2 -120 k2 0.3 ap 25 l "Bezier arc" lphi 270 la 180 lr 19 lp 0.5 1 2 1 h2 0 a1 40 k1 2.8 a2 30 k2 0.8 ap 25 l "Bezier arc" lphi 90 la 0 lp 0.65 4 2 -1 h2 0 w 1 k1 -2 k2 250 ap 25 l "Circular arc" c Red lc OrangeRed 3 4 1 p Dashed h2 0 w 2 c OliveGreen ap 25 l "Straight arc" lc PineGreen 1 3 1 p Dashed h2 0 w 5 k1 -1 k2 -20 ap 25 l "Oval arc" c Brown lc Black 3 3 -1 h1 6 w 1 h2 12 k1 -2 k2 -15 ap 0.5 l "Circular loop" c Red lc OrangeRed lphi 270 la 180
This can be read into igraph with
>g <- read.graph(file="LINKS.NET", format="pajek")
Vertices: 4 Edges: 7 Directed: FALSE Edges:  0 -- 0  0 -- 1  0 -- 1  1 -- 3  2 -- 3  0 -- 2  2 -- 2
 "Blue" "" "" "Red" "OliveGreen"  "Brown" "Red"
When writing a Pajek file with
vertex and edge attributes in the previous two tables are written to
the file if they're present in the graph.
The Pajek file format is included in igraph to allow users to convert their Pajek files to formats more suitable for igraph, like GraphML for example. The Pajek format is not intended to be used as a standard file format of igraph because of the lack of proper documentation.
GraphML is an XML-based file format (an XML application in the XML terminology) to describe graphs. It is a modern format, and can store graphs with an extensible set of vertex and edge attributes, and generalized graphs which igraph cannot handle. Thus igraph supports only a subset of the GraphML language:
Hypergraphs are not supported.
Nested graphs are not supported.
Mixed graphs, ie. graphs with both directed and undirected edges
are not supported.
read.graph() sets the
graph directed if this is the default in the GraphML file, even if
all the edges are in fact undirected.
See the GraphML homepage for more information about the GraphML format.
|<< Random Graphs||Graph Visualization >>|