Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Let's take a look at representing a network in R using the network object from the statnet Statnet package. To get the statnet Statnet package, see R Toolkit.

We'll construct a 4 level network – a root, some departments, some people in each department and some grants of each of the people. We'll add attributes to these objects and display the results.

...

We need to start somewhere. Let's create a new network called "net" with a single node or vertex.

No Format

 #
 # Learning to use network and network plotting functions for VIVO data
 #
 # M. Conlon April 11, 2011
 #
 # 
 # start with a root node
 net <-network.initialize(1,directed=F)
 net %v% "type" <- "Root"
 net %v% "vertex.names" <- "COM"
 net %v% "amt" <-0

...

Next, let's create three departments. Two lines of code are needed for each department. First, add a vertex to the network. Note that that the same attributes are being set for each vertex – type,vertex.names and amt. Second, the vertex is linked to the root by asserting an edge connecting the department to the root.

No Format

 #
 # create some departments
 #
 add.vertices(net,1,list(list(type="Department",vertex.names="Radiology",amt=0)))
 net["COM","Radiology"]<-1
 add.vertices(net,1,list(list(type="Department",vertex.names="Pathology",amt=0)))
 net["COM","Pathology"]<-1
 add.vertices(net,1,list(list(type="Department",vertex.names="Pediatrics",amt=0)))
 net["COM","Pediatrics"]<-1

...

Let's add people to each of the departments. We use the same method as previously – create a vertex for each person and connect it to the department vertex.

No Format

 #
 # create some people in the departments
 #
 add.vertices(net,1,list(list(type="Person",vertex.names="Mancuso",amt=0)))
 net["Radiology","Mancuso"]<-1
 add.vertices(net,1,list(list(type="Person",vertex.names="Sistrom",amt=0)))
 net["Radiology","Sistrom"]<-1 
 add.vertices(net,1,list(list(type="Person",vertex.names="Rathe",amt=0)))
 net["Radiology","Rathe"]<-1 

...

Experienced R programmers will ask "Do we need loops? Why not add all the Pediatric vertices at once and then all the Pediatric edges at once?" And the answer is "Of course." We have simplified the code for the presentation here. R programmers would figure out how to add 15 Pediatric people in two lines – one for vertices and one for edges.

No Format

 for (i in 1:15) {
   name <- paste("P",i,sep="")
   add.vertices(net,1,list(list(type="Person",vertex.names=name,amt=0)))
   net["Pathology",name]<-1
 }
 for (i in 1:30) {
   name <- paste("M",i,sep="")
   add.vertices(net,1,list(list(type="Person",vertex.names=name,amt=0)))
   net["Pediatrics",name]<-1
 }

...

Again, an experienced R programmer could remove steps. The code shown here is designed for clarity, not conciseness nor speed.

No Format

 #
 # Add grants as another level
 #
 vt<-get.vertex.attribute(net,"type")
 vn<-get.vertex.attribute(net,"vertex.names")
 va<-get.vertex.attribute(net,"amt")

 for (i in 1:network.size(net)) {
   if (vt[i] == "Person") {
     di<-get.neighborhood(net,i)[1]  # get department index for this person
     ng <- floor(10*runif(1))   
     if (ng > 0) {
       for (j in 1:ng) {
         gname <- paste(vn[i],".G",j,sep="")
         gamt=floor(50000*rexp(1)*rexp(1))
         add.vertices(net,1,list(list(type="Grant",vertex.names=gname,amt=gamt)))
         net[vn[i],gname]<-1
 #
 # Add the grant amounts to the corresponding person grant total
 #
         va<-get.vertex.attribute(net,"amt")
         va[i]<-va[i]+gamt
         set.vertex.attribute(net,"amt",va)
 #
 # Add the grant amounts to the corresponding department total
 #
         va<-get.vertex.attribute(net,"amt")
         va[di]<-va[di]+gamt
         set.vertex.attribute(net,"amt",va)
 #
 # Add the grant amount to the root total
 #
         va<-get.vertex.attribute(net,"amt")
         va[1]<-va[1]+gamt
         set.vertex.attribute(net,"amt",va)
       }
     }
   }
 } 

...

Now we can display the network using the built-in features of R and the statnet package. A plot can be generated with a single statement:

No Format

 plot(net)

The result is shown below:

Image Removed Default network visualization

Image Added

All the nodes are the same color. Perhaps we'd like to see the grants colored differently from the people and so on. This is easily done. The plot() function can use vertex attributes to control display. For example:

No Format

 plot(net,vertex.col="type")

The result is shown below:

Image Modified

Vertices colored by type. Departments are black. The root is blue

Finally, the code below assigns colors to vertices using the R palette functions (a rainbow palette). The plot displays the size of each node based on the value of the vertex attribute amt. So the root node will be biggest. A formula is used to scale the node sizes based on the log of grant dollars – so 1 million dollars has size "6", a one thousand dollar grant has size "3", and so on.

No Format

 #
 #  generate a plot of departments, people and grants
 #

 palette(rev(rainbow(6)))
 plot(net,vertex.col="type",
   vertex.cex=0.25*(1+floor(log10(1+get.vertex.attribute(net,"amt")))))

The result is shown below:

Image Modified

Vertices by Type and Amt. Larger vertices represent more grant dollars. Note some very small "people" vertices – these have no associated grants and therefore no grant dollars.

...