I would like to adjust the position of a second nodeset based on the given coordinates of a first nodeset for a two-mode plot with igraph. Is there a convenient way of doing so?
Here is a replicable example that shows what I would like to do.
library(igraph)
set.seed(01)
# get sample matrix
bip <- matrix(sample(0:1,10*20, replace=TRUE),10,20)
# transform to bipartite incidence matrix for plotting
g <- graph_from_incidence_matrix(bip)
# initialise empty matrix for coords of the first nodeset
layout_given <- matrix(0, 10, 2)
# sample some coordinates that represent those which are given
layout_given[,1] <- sample(-2.435651:3.670977,10, replace = T)
layout_given[,2] <- sample(-2.435651:3.670977,10, replace = T)
# these are the standard positions of the nodes the algorithm
# layout.fruchterman.reingold assigns
layout_fr = layout.fruchterman.reingold(g)
# replace the given positions with those generated by the algorithm.
# these are the first 10 in the layout 30x2 layout matrix
# (because there are 10 nodes of the first nodeset) with those that I would like
# to hold fixed
layout_fr[1:10,] <- layout_given
plot(g,
layout = layout_fr,
vertex.shape= c("circle", "square")[V(g)$type+1],
vertex.color= c("red", "orange")[V(g)$type+1])
The plot shows that positioning of the second nodeset is not optimised for readability with regards to the first nodeset. This interferes with the readability of the graph.
How can I optimise the position of the second nodeset based on the given positioning in the first nodeset layout_given
?
There must be a way to do that but my coding knowledge is too limited to understand the way layout.fruchterman.reingold()
is coded to understand how to apply it conditional on the given coordinates for the first nodeset.
layout_as_bipartite()
actually helps because the real network that I have consists of about 30 nodes of the first node type and more than 200 of the second nodeset, so it is difficult to see which nodes of the first node type are connected with the second node type. Any further suggestions? In addition, I have second network one-mode network that I plot underneath, so I guess it would be better to usefruchtermann.reingold
?