get.graph.attribute(graph, name)
set.graph.attribute(graph, name, value)
remove.graph.attribute(graph, name)
get.vertex.attribute(graph, name, index=V(graph))
set.vertex.attribute(graph, name, index=V(graph), value)
remove.vertex.attribute(graph, name)
get.edge.attribute(graph, name, index=E(graph))
set.edge.attribute(graph, name, index=E(graph), value)
remove.edge.attribute(graph, name)value is longer.get.graph.attribute, get.vertex.attribute and
get.edge.attribute return an R object, or a list of R objects
if attributes of more vertices/edges are requested. set.graph.attribute, set.vertex.attribute,
set.edge.attribute, and also remove.graph.attribute,
remove.vertex.attribute and remove.edge.attribute return
a new graph object with the updates/removes performed.
list.graph.attributes, list.vertex.attributes and
list.edge.attributes return a character vector, the names of
the attributes present.
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.
There is a simpler notation for using graph attributes: the
$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.
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.
print.igraph can print attributes. See
attribute.combination for details on how igraph combines
attributes if several vertices or edges are mapped into one.g <- graph.ring(10)
g <- set.graph.attribute(g, "name", "RING")
# It is the same as
g$name <- "RING"
g$name
g <- set.vertex.attribute(g, "color", value=c("red", "green"))
get.vertex.attribute(g, "color")
g <- set.edge.attribute(g, "weight", value=runif(ecount(g)))
get.edge.attribute(g, "weight")
# The following notation is more convenient
g <- graph.star(10)
V(g)$color <- c("red", "green")
V(g)$color
E(g)$weight <- runif(ecount(g))
E(g)$weight
print(g, g=TRUE, v=TRUE, e=TRUE)Run the code above in your browser using DataLab