Reputation: 41
I've been playing around with igraph in R and am having trouble using weights when I visualize a network. I have read that this may not work with every layout type but should with Fruchterman-Reingold.
My code and output are below (I tried two different versions of the layout function, I think they are doing the same thing, but tried both just in case)
I would expect Cecil and Bob to be very close together in vers1 because of the high weighting on their relationship, but that doesn't seem to happen. Only when I create additional rows for Bob and Cecil (vers2) does this seem to occur, but that's going to be a pain for what I really want to do with a much larger data set.
I would post the images of what I'm getting, but I'm new to stack overflow and didn't have enough reputation points.
Any ideas? Thanks 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)
Upvotes: 2
Views: 887
Reputation: 1109
Here is a version using sna
and network
libraries. I recoded your value of 100 to 3 to make it into something not physically impossible. But you can use log values or something. Notice also needed to transform values from similarities to distances.
library(sna)
library(network)
# recode the desired distances to something more reasonable
# (can't phiscally have one distance 100X the 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 to 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 from distances to similarities
adjMat[adjMat!=0]<-4-adjMat[adjMat!=0]
# construct an appropriate geodesic distance matrix from the similarities
distMat<-geodist(adjMat,ignore.eval=FALSE,inf.replace = sqrt(network.size(relNet)))$gdist
# compute coords using distance matrix and 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')
Upvotes: 1