To build a network chart, your data must be formatted in one of these formats:


A-B-C-D, E-A-E-A
1- Adjacency matrix
An adjacency matrix is a square matrix where individuals are the same in row and columns of the matrix. It’s typically the kind of matrix you get when calculating the correlation between each pair of individual. In this example, we have 1 connection from E to C, and 2 connections from C to E. By default, we get an unweighted and oriented network.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#library library(igraph) set.seed(10) # Create data data=matrix(sample(0:2, 25, replace=TRUE), nrow=5) colnames(data)=rownames(data)=LETTERS[1:5] # Tell Igraph it is an adjency matrix... with default parameters set.seed(10) network=graph_from_adjacency_matrix(data) # plot it plot(network) |
Note that you can ask for weighted or unweighted network, and directed or undirected network. Following your choice, you need to specify how you want to interpret the adjency matrix: e.g. how to calculate the weight? Using the sum of the connection? The max? .. Type help(graph_from_adjacency_matrix) for more details.
1 2 3 4 5 6 7 8 9 10 11 |
# left par(mfrow=c(1,2)) set.seed(10) network=graph_from_adjacency_matrix(data, weighted=NULL) plot(network, main="UNweighted") # right set.seed(10) network=graph_from_adjacency_matrix(data, weighted=TRUE) plot(network, main="weighted") |
2- Incidence matrix
An incidence matrix does not necessarily have the same individuals in row and colum. By default, it is directed from rows to columns.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
# create data library(igraph) set.seed(1) data=matrix(sample(0:2, 15, replace=TRUE), nrow=3) colnames(data) <- letters[1:5] rownames(data) <- LETTERS[1:3] # create the network object set.seed(1) network=graph_from_incidence_matrix(data) # plot it plot(network) |
3- Edge List
The edge list is a data.frame listing all the connections.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
# create data: links=data.frame( source=c("A","A", "A", "A", "A","F", "B"), target=c("B","B", "C", "D", "F","A","E") ) # create the network object set.seed(10) network=graph_from_data_frame(d=links, directed=F) # plot it plot(network) |
You can add a second data frame which provides some features concerning each nodes. This kind of additional data can be useful to custom the network. Here the size of nodes depends of the “carac” column value. Note that you can make the chart directed or undirected.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
# You can provide informations concerning nodes! nodes=data.frame( name=LETTERS[1:6], carac=c( rep(10,3), rep(30,3)) ) # Turn it into igraph object network=graph_from_data_frame(d=links, vertices=nodes, directed=F) # And use these new info in the plot! plot(network, vertex.size=nodes$carac) # The same but directed: network=graph_from_data_frame(d=links, vertices=nodes, directed=T) plot(network, vertex.size=nodes$carac) |
4- Literal List of connections
The last option is to provide a vector with all the connections listed.
Type help(graph_from_literal) for more information.
1 2 3 4 |
network=graph_from_literal( A-B-C-D, E-A-E-A, D-C-A, D-A-D-C ) plot(network) |
Not what you are looking for ? Make a new search ![mediatagger]
Leave a Reply
Be the First to Comment!