0

Despite the relative popularity of neighborNets, I couldn't find any solutions to the following problem. In R, I'm trying to plot a neighborNet (created using package phangorn, object class networx). The package uses igraph for plotting static 2D graphs, so every time I re-plot the graph, the layout changes/rotates (default behaviour of igraph, apparently). Now, if I were to plot an usual igraph, I'd just save the layout, and then keep re-using it:

fixed = layout.sphere(somegraph)
plot(somegraph, layout=fixed)

But this doesn't work for the current problem. The help file of plot.networx does refer to igraph and layout, but only in the 'See also' section. Creating a x-y coordinate matrix manually wouldn't work (as suggested here), the location of the node labels/tips of nodes is significant on a neighbornet. I tried

library("phangorn")
library("igraph")
mydist = dist(matrix(sample(100), ncol=10))  # example data
nnet = neighborNet(mydist)
fixed = layout.sphere(nnet)  # error, Not a graph object
fixed = layout.sphere(as.igraph(nnet))  # doesn't work properly, mangled graph

So the question, how to get it working in the vein of

plot.networx(nnet, type="2D", layout = fixed)  # ?
Community
  • 1
  • 1
user3554004
  • 1,044
  • 9
  • 24

1 Answers1

0

plot.networx calls phangorn:::coords to get the coords which (for 2D) creates an igraph object and then uses layout.kamada.kawai. This is all hard-coded into plot.networx so not flexible enough such that you could use another layout algorithm.

You could follow the code in phangorn:::coords to create the graph layout and replace the layout function with another one, and then call phangorn:::plot2D with those coords. See plot.networx to see how to call phangorn:::plot2D correctly.

Spacedman
  • 92,590
  • 12
  • 140
  • 224
  • I was starting to look into the code as well, but was hoping there would be a "nicer" solution, since this all is actually for pedagodical purposes (I suppose I should have mentioned that). But anyhow, `xy=phangorn:::coords(nnet); phangorn:::plot2D(xy, nnet)` does exactly what I asked for. – user3554004 Jul 09 '15 at 17:26