timelyportfolio/d3r

add igraph to d3 converter

timelyportfolio opened this issue · 5 comments

While talking with @ttriche on Twitter, I remembered a helpful function would be to convert igraph to a "standard" d3.js network. I have some preliminary code that will require testing and iteration.

mine looks something like this (pulling out part of a protein-protein network):

# pull out CREBBP for discussion 
library(networkD3)
toD3 <- function(g) { 
  wc <- cluster_walktrap(g) 
  members <- membership(wc)
  igraph_to_networkD3(g, group=members)
}
plotForceNet <- function(g, ...) {
  stopifnot(is(g, "igraph"))
  with(toD3(g), 
       forceNetwork(Links=links, Nodes=nodes, 
                    Source="source", Target="target", 
                    NodeID="name", Group="group", 
                    zoom=TRUE, bounded=TRUE, ...))
}
CREBBP_sub <- induced_subgraph(mergedGraph, 
                               ego(mergedGraph, 1, 
                                   which(names(V(mergedGraph))=="CREBBP"))[[1]])
p <- plotForceNet(CREBBP_sub, fontSize=12, opacity=0.9, opacityNoHover=0.3)
saveNetwork(p, file="CREBBP.html") # uploaded to amazon S3

Thanks @ttriche. The networkd3::igraph_to_networkD3 works fairly well, but it makes some assumptions for the benefit of its particular use. I think the proposed code is a little more generic, but I'm not convinced. I have not tested with the member/group, so I will do that now to make sure it handles. After that, my next round of tests will be to plug in the converted to multiple examples.

Add layout if available to allow igraph layouts to be paired with d3.js. This will make for a very powerful combination supplementing d3.js with the igraph layouts, but interactivity will be tricky. Start with just adding x and y attributes and then go from there.

added attributes to the return value c200062

Going to close this for now, since I added d3_igraph, but I will very likely resurrect this especially given @thomasp85 work on tidygraph.