r - arranging matrix - network graphs -
i trying make network graph, using function gplot
library(sna)
. graph represent links between different fields. have 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" , "mcli" "fields of interest" , every row different project has some/any/none of fields in common. how transform these data this?
matx: mtm fi mcli mtm 10 0 1 fi 0 1 1 mcli 10 1 17
i interested in representing -in network graph- fields "nodes", , connections "edges". helpful in representing "popular" , interconnected fields. possible these data?
thanks in advance!
edit: came across this solution, ok want:
library(igraph) g<-graph.incidence(as.matrix(mat1),weighted=true,directed=false) summary(g) plot(g)
here 1 way make network graph data each node "field of interest". note have made symmetrical adjacency matrix original data doesn't entirely match desired matrix output.
library(igraph) # use matrix multiplication create symmetrical adjacency matrix. adj_mat = t(mat1) %*% (mat1) # 2 ways 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()
Comments
Post a Comment