Question

I was trying to make a network graph, using the function gplot from library(sna). The graph would represent the links between different fields. I have the following data:

MTM <- c(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1)
FI <- c(0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)
MCLI <- c(0,0,1,0,0,1,1,1,0,0,0,0,1,0,1,1,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1)
mat1 <- data.frame(MTM,FI,MCLI)
mat1 <- as.matrix(mat1)

Where "MTM", "FI" and "MCLI" are the "fields of interest" and every row is a different project that has some/any/none of the fields in common. How could I transform these data to look like this?

matx:   
       MTM   FI   MCLI
MTM     10    0   1
FI       0    1   1
MCLI    10    1   17

I am interested in representing -in a network graph- the fields as "nodes", and the connections as "edges". This could be helpful in representing the most "popular" and interconnected fields. Is it possible with these data?

Thanks in advance!

EDIT: I came across this solution, which could be OK for what I want:

library(igraph)
G<-graph.incidence(as.matrix(mat1),weighted=TRUE,directed=FALSE)
summary(G)
plot(G)
Was it helpful?

Solution

Here is one way to make a network graph from your data where each node is a "field of interest". Note that I have made a symmetrical adjacency matrix from your original data that doesn't entirely match your desired matrix output.

library(igraph)

# Use matrix multiplication to create symmetrical adjacency matrix.
adj_mat = t(mat1) %*% (mat1)

# Two ways to show edge weights.
png("igraphs.png", width=10, height=5, units="in", res=200)
par(mfrow=c(1, 2))

g1 = graph.adjacency(adj_mat, mode="undirected", diag=FALSE, weighted=TRUE)
plot(g1, edge.width=E(g1)$weight, vertex.size=50)

g2 = graph.adjacency(adj_mat, mode="undirected", diag=FALSE)
plot(g2, vertex.size=50)

dev.off()

enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top