Tag Archives: statnet

Plotting Networks in R

Using the network package, you can plot graphs in a flexible and powerful way.  Often, when plotting a network, we want to vary the color, size, or shape of the vertices based on some attributes.  Let's say that we have a freewheeling sexual network (easier to simulate) and we would like to color the vertices of the graph according to their HIV sero-status.  Let's also say that we want to make the shape of each vertex reflect the sex of the individual.  We use the following code:

R:
  1. # begin with randomly constructed edgelist
  2. set.seed(12345)
  3.  
  4. n1 <- round(1+10*runif(50))
  5. n2 <- round(1+10*runif(50))
  6. eee <- cbind(n1,n2)[order(n1),]
  7. net <- network(eee,directed=FALSE) # this will be a dense network!
  8. hiv <- rbinom(50,size=1,prob=0.2)  # random infections!
  9. sex <- rbinom(50,size=1,prob=0.5)  # random sexes!
  10. set.vertex.attribute(net,"hiv",hiv+1)
  11. set.vertex.attribute(net,"sex",sex+3)
  12.  
  13. ## now plot
  14. plot(net,
  15. vertex.col="hiv",
  16. vertex.sides="sex",
  17. vertex.cex=5,
  18. vertex.rot=-30,
  19. edge.lwd=1)

I definitely wouldn't want to be part of that party.

Extracting adjacency matrices with valued edges

This may seem obvious to an expert statnet user, but it took me a bit of careful reading of Carter's paper and some trial and error to figure it out. We are using the frequency of behaviors based on ethological observations as edge weights and would like to be able to extract a matrix of the edge weights.

R:
  1. set.seed(123)
  2. ## generate a network with 21 nodes and 50 edges.
  3. ## some edges are either self-loops or redundant
  4. ## just a quick and dirty way to get an example network object
  5.  
  6. n1 <- round(1+20*runif(50))
  7. n2 <- round(1+20*runif(50))
  8. n3 <- rpois(50,3)
  9. eee <- cbind(n1,n2)[order(n1),]
  10. net <- network(eee,directed=FALSE)
  11. set.edge.attribute(net,"meaningful.measure",n3)
  12. as.matrix(net,attrname="meaningful.measure")

This last command returns a 50x50 matrix of the edge weights.

Extracting an edge list from a network object

I've been using the statnet suite of tools a lot recently.  As with any powerful software, there is quite a learning curve.  I will write some notes in my blog to help me remember tricks that I learn along the way.  And who knows? They might even be useful to other people!

For a variety of reasons, we have found it easy to import our networks as adjacency matrices.  The problem is that when there are attributes associated with the edges, it is much easier to deal with an edge list.  While using summary(net) yields an edge list as part of the summary, it was not clear to me how to get such a list as a manipulable object.  I wrote the statnet_help list and Carter Butts (co-author of network) pointed out to me that getting an edgelist is quite simple. Having read in our adjacency matrix

R:
  1. mat <-read.table("mat5.txt",header=TRUE)
  2. mat <- as.matrix(mat)
  3. net <- network(mat, directed=FALSE)

We can get the edge list like this:

R:
  1. bbb <- as.matrix(net,matrix.type="edgelist") 

Easy...