R igraph Visualizing weighted connections -
i've been playing around igraph in r , having trouble using weights when visualize network. have read may not work every layout type should fruchterman-reingold.
my code , output below (i tried 2 different versions of layout function, think doing same thing, tried both in case)
i expect cecil , bob close in vers1 because of high weighting on relationship, doesn't seem happen. when create additional rows bob , cecil (vers2) seem occur, that's going pain want larger data set.
i post images of i'm getting, i'm new stack overflow , didn't have enough reputation points.
any ideas? in advance.
code:
#vers1 library(igraph) relations <- data.frame(from=c("bob", "cecil", "cecil", "david", "david", "esmeralda"), to=c("alice", "bob", "alice", "alice", "bob", "alice"), weight=c(1,100,1,1,1,1)) graph<-graph_from_data_frame(relations, directed=f) coords1<-layout_with_fr(graph, weights=e(graph)$weight) coords2 <- layout.fruchterman.reingold(graph, weights=e(graph)$weight); plot(graph,layout=coords1) plot(graph,layout=coords2) #vers2 library(igraph) relations <- data.frame(from=c("bob", "cecil", "cecil", "david", "david", "esmeralda", "cecil", "cecil", "cecil", "cecil", "cecil"), to=c("alice", "bob", "alice", "alice", "bob", "alice", "bob", "bob", "bob", "bob", "bob"), weight=c(1,1,1,1,1,1,1,1,1,1,1)) graph<-graph_from_data_frame(relations, directed=f) coords1<-layout_with_fr(graph, weights=e(graph)$weight) coords2 <- layout.fruchterman.reingold(graph, weights=e(graph)$weight); plot(graph,layout=coords1) plot(graph,layout=coords2)
here version using sna
, network
libraries. recoded value of 100 3 make not physically impossible. can use log values or something. notice needed transform values similarities distances.
library(sna) library(network) # recode desired distances more reasonable # (can't phiscally have 1 distance 100x others) relations <- data.frame(from=c("bob", "cecil", "cecil", "david", "david", "esmeralda"), to=c("alice", "bob", "alice", "alice", "bob", "alice"), weight=c(1,3,1,1,1,1)) # convert network object including edge weights relnet<-network(relations,ignore.eval = false,names.eval='weight',matrix.type='edgelist',directed=false) # valued construct adjacency matrix adjmat<-as.matrix(relnet,attrname='weight') # convert distances similarities adjmat[adjmat!=0]<-4-adjmat[adjmat!=0] # construct appropriate geodesic distance matrix similarities distmat<-geodist(adjmat,ignore.eval=false,inf.replace = sqrt(network.size(relnet)))$gdist # compute coords using distance matrix , kk algorithm coords<-network.layout.kamadakawai(relnet,layout.par=list(elen=distmat)) # plot using precomputed coords plot(relnet,displaylabels=true,coord=coords,edge.label='weight',edge.lwd='weight')
Comments
Post a Comment