2

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)
Josh Gredvig
  • 41
  • 1
  • 4
  • hi josh, does this answer help you http://stackoverflow.com/questions/5968612/how-do-i-lengthen-edges-in-an-igraph-network-plot-layout-fruchterman-reingold ? – bjoseph Jul 22 '15 at 14:37
  • Thanks bjoseph. I don't think that's quite what I'm looking for. That seems to be spacing out an entire graph, I'm trying to make sure people with higher weighted relationships are closer together relative to smaller weighted relationships. – Josh Gredvig Jul 22 '15 at 14:42
  • sorry :(, never used igraph, but i have been able to control weights in this package https://christophergandrud.github.io/networkD3/ – bjoseph Jul 22 '15 at 15:08
  • Depending on which version of igraph you use, you might have run into this: https://github.com/igraph/igraph/issues/839 – Gabor Csardi Jul 22 '15 at 16:16

1 Answers1

1

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')

enter image description here

skyebend
  • 1,079
  • 6
  • 19