There are three types of attributes in igraph: graph, vertex and edge
attributes. Graph attributes are associated with graph, vertex
attributes with vertices and edge attributes with edges. Examples for graph attributes are the date when the graph data was
collected or other types of memos like the type of the data, or
whether the graph is a simple graph, ie. one without loops and
multiple edges.
Examples of vertex attributes are vertex properties, like the
vertex coordinates for the visualization of the graph, or other
visualization parameters, or meta-data associated with the vertices,
like the gender and the age of the individuals in a friendship
network, the type of the neurons in a graph representing neural
circuitry or even some pre-computed structual properties, like the
betweenness centrality of the vertices.
Examples of edge attributes are data associated with edges: most
commonly edge weights, or visualization parameters.
In recent igraph versions, arbitrary R objects can be assigned as
graph, vertex or edge attributes.
Some igraph functions use the values or graph, vertex and edge
attributes if they are present but this is not done in the current
version very extensively. Expect more in the (near) future.
Graph attributes can be created with the set.graph.attribute
function, and removed with remove.graph.attribute
. Graph
attributes are queried with get.graph.attribute
and the
assigned graph attributes are listed with
list.graph.attributes
.
The function graph.attributes
returns all graph attributes in a
named list. This function has a counterpart that sets all graph
attributes at once, see an example below.
There is a simpler notation for using graph attributes: the
$
operator. It can be used both to query and set graph
attributes, see an example below.
The functions for vertex attributes are
set.vertex.attribute
, get.vertex.attribute
,
remove.vertex.attribute
and list.vertex.attributes
and
for edge attributes they are set.edge.attribute
,
get.edge.attribute
, remove.edge.attribute
and
list.edge.attributes
.
Similarly to graph attributes, vertex.attributes
returns all
vertex attributes, in a named list, and edge.attributes
returns
all edge attributes, in a named list.
There is however a (syntactically) much simpler way to handle vertex
and edge attribute by using vertex and edge selectors, it works like this:
V(g)
selects all vertices in a graph, and V(g)$name
queries the name
attribute for all vertices. Similarly is
vs
is a vertex set vs$name
gives the values of the
name
attribute for the vertices in the vertex set.
This form can also be used to set the values of the attributes, like
the regular R convention:
V(g)$color <- "red"
It works for vertex subsets as well:
V(g)[1:5]$color <- "green"
The notation for edges is similar: E(g)
means all edges
E(g)$weight
is the weight
attribute for all edges, etc.
See also the manual page for iterators
about how to create
various vertex and edge sets.