Graph, vertex and edge attributes

Attributes are associated values belonging to a graph, vertices or edges. These can represent some property, like data about how the graph was constructed, the color of the vertices when the graph is plotted, or simply the weights of the edges in a weighted graph.

graph <- add.graph.attribute(graph, attrname)
graph <- remove.graph.attribute(graph, attrname)
get.graph.attribute(graph, attrname=NULL)
set.graph.attribute(graph, attrname, value)
g.a(graph, attrname=NULL)
g.a(graph, attrname) <- value
graph <- add.vertex.attribute(graph, attrname)
graph <- remove.vertex.attribute(graph, attrname)
get.vertex.attribute(graph, attrname=NULL, v=igraph.vs.all(graph))
set.vertex.attribute(graph, attrname, v=igraph.vs.all(graph), value)
v.a(graph, attrname=NULL, v=igraph.vs.all(graph))
v.a(graph, attrname, v=igraph.vs.all(graph)) <- value
graph <- add.edge.attribute(graph, attrname)
graph <- remove.edge.attribute(graph, attrname)
get.edge.attribute(graph, attrname=NULL,
set.edge.attribute(graph, attrname,, value)
e.a(graph, attrname=NULL,
e.a(graph, attrname, <- value
The graph object to work on. Note that the original graph is never modified, a new graph object is returned instead; if don't assign it to a variable your modifications will be lost! See examples below.
Character constant, the name of the attribute. For query functions this can be NULL, in this case the list of the attributes will be returned.
Numeric vector, the ids of the vertices. It is not recycled, even if value is longer.
Numeric vector, the new value(s) of the attributes, it will be recycled if needed.
Numeric vector, the ids of the edges. It is not recycled, even if value is longer.

An attribute value is either a numeric constant or a character string. Each attribute has a name, this is simply a character string. The names of the graph, vertex and edge attributes use different name spaces, so the color vertex attribute has nothing to do with the color edge attribute.

In order to use an attribute it has to be added first by using the add.*.attribute functions. The initial values of the attributes are undefined. After an attribute is added, its value(s) can be set by the set.*.attribute functions and queried by the get.*.attribute functions.

You can also use the shorter g.a, v.a, e.a forms, see the examples below, they are shorthands for the get.*.attribute and set.*.attribute functions, the latter is in effect if it is used on the left hand side of an assignment, see examples below.

An unneeded attribute can be removed by the remove.*.attribute functions.

You can list the attributes of a graph by giving attrname=NULL to the query functions (this is the default), or by print.igraph.


  • The add.*.attribute and remove.*.attribute and set.*.attribute functions all return a new graph object with the requested modifications.

    The get.*.attribute functions return numeric or character vectors, the values of the attributes if the attrname argument is not NULL and a character vector, the name of the attributes if it is NULL.


Only numeric constant attributes are implemented right now, string attributes will be added shortly.

See Also

print.igraph can also print attributes

  • attributes
  • add.graph.attribute
  • remove.graph.attribute
  • get.graph.attribute
  • set.graph.attribute
  • g.a
  • g.a<-
  • add.vertex.attribute
  • remove.vertex.attribute
  • get.vertex.attribute
  • set.vertex.attribute
  • v.a
  • v.a<-
  • add.edge.attribute
  • remove.edge.attribute
  • get.edge.attribute
  • set.edge.attribute
  • e.a
  • e.a<-
g <-
g <- add.graph.attribute(g, "id")
g.a(g, "id")
remove.graph.attribute(g, "id") ## g is unchanged
g.a(g, "id")

g <- add.vertex.attribute(g, "size")
v.a(g, "size") <- c(1,2)
v.a(g, "size")

g <- add.edge.attribute(g, "weight")
e.a(g, "weight") <- sample(1:100, 20, replace=TRUE)
e.a(g, "weight")

print.igraph(g, g=TRUE, v=TRUE, e=TRUE)

g2 <-
g2 <- add.graph.attribute(g2, "date", "character")
g.a(g2, "date")
g.a(g2, "date") <- "2005/10/25"
g.a(g2, "date")
remove.graph.attribute(g2, "date") ## g2 is unchanged
g.a(g2, "date")

g2 <- add.vertex.attribute(g2, "id", "character")
abc <- strsplit("abcdefghujklmnpqrstuvwxyz", "")[[1]]
v.a(g2, "id") <- abc
v.a(g2, "id")

g2 <- add.edge.attribute(g2, "random", "character")
e.a(g2, "random") <- sample(1:100, 20, replace=TRUE)
e.a(g2, "random")

print.igraph(g2, g=TRUE, v=TRUE, e=TRUE)
Documentation reproduced from package igraph, version 0.1.2, License: GPL version 2 or later (June, 1991)

Community examples

Looks like there are no examples yet.