Graph difference (tutorial)

From visone user support

Jump to: navigation, search

This tutorial shows, how the R console can be used to manipulate and compare graphs beyond the functionality that visone provides.


We'll assume in the following, that you have two networks on the same set of nodes and want to find out which of their edges differ. An important precondition is, that there exist an identifying attribute that:

This attribute will be used in the following to establish the relation between the two networks. For this tutorial we assume it is called nodeid and is of type text.

Creating the difference network

Select the tab containing your first network and open the R console. Give your network a name in the input field next to the send button and press "send". For this tutorial we'll assume that it is named g1.

Now the command ls() should list g1. ls() can always be used to list all objects in the current R environment.

Send your second network to the R console: select it in the main window, name it (g2 in this tutorial), and press "send". The command ls() should now show the two networks.

The idea of this tutorial is, to create the difference network by subtraction of the networks adjacency matrices. Therefore, it is first of all necessary, to get those in the correct node order. We'll achieve this by deriving a node order from the attribute identifying the nodes (nodeid).

The order of the nodes in g1 is derived by: order1<-sort(V(g1)$nodeid,index.return=T)$ix.

This command combines a number of things, let's visit them in detail:

We do the same for the second network with order2<-sort(V(g2)$nodeid,index.return=T)$ix. The two lists order1 and order2 can now be used as ordering permution on the node sets of the two networks, i.e. they put the nodes of the two networks in the same order. As an example V(g1)$nodeid[order1] lists the node ids in the just established order and should be exactly the same as V(g2)$nodeid[order2], i.e. the node ids of the second network in correct order.

To get the adjacency matrix of a network we would use the command a1<-get.adjacency(g1). Here we want these matrices in our node order and thus reorder them accordingly by a1<-get.adjacency(g1)[order1,order1]. The "[order1,order1]"-part just reorders rows and columns of the matrix. Use the two commands a1<-get.adjacency(g1)[order1,order1] and a2<-get.adjacency(g2)[order2,order2] to create a1 and a2, the two adjacency matrices of your networks in correct node order.

The difference between those can then easily be derived as the entrywise matrix difference: diff<-a2-a1. diff is the matrix with entry -1 for an edge that was deleted, 1 for an added edge and 0 if the edge did not change.

Now we can create the network with only the changed edges: diffnetwork<-graph.adjacency(diff, weighted="change"). The function graph.adjacency() creates a graph from an adjacency matrix with an edge for every entry that does not equal zero. In that the parameter weighted="change" stores the corresponding values in an edge attribute with the name "change". Before we load that back into visone we want to reattach the node ids with: V(diffnetwork)$nodeid<-V(g1)$nodeid[order1]

Finally, you can select the newly created network "diffnetwork" in the combobox of the dialog (next to the "load network" button) and load it into visone using the "load network" button.

Visualizing the difference

Close the console and you should see your network of changed edges. Assuming, that your networks where undirected, every edge will be shown as to opposing, directed edges. Use the "transformation"-tab and the "links" and "merge" with the option "contrary directed" to merge those into undirected edges.

Back to the undirected graph we do know, which edges changed but not, what happened to them. Using the "change" attribute created in R we can easily visualize what happened:

Personal tools