### 06. Plotting networks: using node attributes

**I tried these codes in igraph v0.6, R 2.15, and all seems to be working fine. Let me know if you have any problems. (DS, Feb 9, 2013)

In this section, I'll run through methods to represent different qualities of nodes (vertices) in a network plot. I will mainly use igraph. For a full list of plotting arguments in igraph, use:

`?igraph.plotting`

Our goal here will be to produce a network figure with different color and size for nodes, like this:

This requires loading vertex attributes in the network. In general, when you construct a network you will have some sort of data on the "attributes" of nodes. In our example, we'll use the sex of individuals as the main attribute that determines the color of the nodes. We'll then set the size of the nodes as the degree of the node (i.e., simply the number of edges that is connected to that node).

To do this, we'll first require a list of attributes that are associated with each node. I have supplied this at the bottom of this page as an attachment called "sample_attributes". As with other files, it is a .csv file. It looks like this:

 Bird.ID Sex 23732 F 23871 F 58009 F 23778 M 23824 M 58098 M 58256 M

Notice that I sorted this by sex so that the Bird.ID (i.e. vertex IDs) are not sorted in order. I did this on purpose since you often will not have the attributes file sorted exactly the way the IDs are arranged in the adjacency matrix.

The first thing to note is that in igraph, verted attributes have the notation
V(graphname)\$attributename

When you load a 'graph object' in igraph, the vertex names are automatically created, so:

`library(igraph)`
`#import the sample_adjmatrix file:`
`dat=read.csv(file.choose(),header=TRUE,row.names=1,check.names=FALSE)`
`m=as.matrix(dat)`
`net=graph.adjacency(m,mode="undirected",weighted=NULL,diag=FALSE)`
`# show the names of the vertices you just imported:`
`V(net)\$name`

`#the result will be:`
`#[1] "23732" "23778" "23824" "23871" "58009" "58098" "58256"`

what you want to do, then, is to assign attributes to these vertices. To assign the sex using your attributes file:

`#import the sample_attributes`
`a=read.csv(file.choose())`
`V(net)\$Sex=as.character(a\$Sex[match(V(net)\$name,a\$Bird.ID)]) # This code says to create a vertex attribute called "Sex" by extracting the value of the column "Sex" in the attributes file when the Bird ID number matches the vertex name.`
`V(net)\$Sex # This will print the new vertex attribute, "Sex"`
`# and you will get:`
`#[1] "F" "M" "M" "F" "F" "M" "M"`

using this, you will then assign colors to the "F" and "M" categories, and move this to a vertex attribute named "color". This is a pre-assigned vertex attribute number. When you fill this, the plot will automatically refer to these values for setting node colors.

`V(net)\$color=V(net)\$Sex #assign the "Sex" attribute as the vertex color`
`V(net)\$color=gsub("F","red",V(net)\$color) #Females will be red`
`V(net)\$color=gsub("M","blue",V(net)\$color) #Males will be blue`
`plot.igraph(net,vertex.label=NA,layout=layout.fruchterman.reingold)`

Now, let's play with the node size. Node size is set through a vertex attribute named "size". I'm going to set up the node size as the degree centrality of the nodes.

` V(net)\$size=degree(net)*5 #because 1 is a small size for a node, I'm just multiplying it by 5`
` plot.igraph(net,vertex.label=NA,layout=layout.fruchterman.reingold)`

** The ALTERNATIVE way to do this is to set up a vector separately from the 'vertex attribute', and assign the color when you run the plot command. This works equally well.
`library(igraph)`
`#import the sample_adjmatrix file:`
`dat=read.csv(file.choose(),header=TRUE,row.names=1,check.names=FALSE)`
`m=as.matrix(dat)`
`net=graph.adjacency(m,mode="undirected",weighted=NULL,diag=FALSE)`
`#import the sample_attributes`
`a=read.csv(file.choose())`
`nodecolor=as.character(a\$Sex[match(V(net)\$name,a\$Bird.ID)])`
`nodecolor=gsub("F","red",nodecolor)`
`nodecolor=gsub("M","blue",nodecolor)`
`nodesize=degree(net)*5`
`plot.igraph(net,vertex.label=NA,layout=layout.fruchterman.reingold, vertex.color=nodecolor,vertex.size=nodesize)`

for a full list of different vertex attributes you can assign this way, refer to look under ?plot.igraph or follow this link.

Some of the other useful attributes that have pre-assigned role inside the plot.igraph() function:

• vertex.shape=##
• label.dist=## ...the distance of the vertex label from the center of the vertex.
• label.degree=##  ...in RADIANS, the angle from the center of the vertex to put the label, so pi/2 would but the label below the vertex.
... similar notation for edge attributes... for example, setting edge.color="black" will make the lines black and perhaps easier to see. Again, for a full list of the different specifications you can make, use ?igraph.plotting.

ċ
Dai Shizuka,
Apr 27, 2012, 8:42 AM
ċ
Dai Shizuka,
Apr 29, 2010, 10:48 AM