Reputation: 197
I am using the below code to generate a visNetwork in circular layout format (image attached below)
visNetwork(data$nodes,data$edges) %>% visIgraphLayout(layout="layout_in_circle")
I want to change the layout in a way such that, green nodes in inner circle and blue nodes in outer circle. Please let me know how to achieve this.
Thanks in advance.
Upvotes: 0
Views: 1040
Reputation: 630
One way is to build the layout in igraph. You can feed it back into visNetwork at the end. I wasn't sure if you wanted the inner circle to connect to the outer. Both options are coded.
Call the green nodes nodes1
and the blue nodes nodes2
, created by subsetting your nodes matrix.
# edges within nodes1
edges1 <- edges[edges$from%in%nodes1$id & edges$to%in%nodes1$id, ]
# edges within nodes2
edges2 <- edges[edges$from%in%nodes2$id & edges$to%in%nodes2$id, ]
# edges within nodes1 or 2 = all edges if nodes1, 2 account for all nodes
edges12 <-edges[edges$from%in%c(nodes2$id,nodes1$id) &
edges$to%in%c(nodes2$id,nodes1$id) , ]
igraph1 <- graph.data.frame(edges1, directed=F, vertices=nodes1)
igraph2 <- graph.data.frame(edges2, directed=F, vertices=nodes2)
# inner circle doesn't connect to outer:
igraph12a <- graph.data.frame(rbind(edges1, edges2), directed = F,
vertices = rbind(nodes1, nodes2))
# inner circle can connect to outer:
igraph12b <- graph.data.frame(edges12, directed = F, vertices =
rbind(nodes1, nodes2))
l1 = layout.circle(igraph1)
l2 = layout.circle(igraph2)
l12 = rbind(l1, l2 * 0.5) # 0.5 term scales the inner circle size
# plot both circles
plot.igraph(igraph1, layout = l1, rescale = F)
plot.igraph(igraph2, layout = l2*.5, add = T, rescale = F)
# plot both circles at once
plot.igraph(igraph12a, layout = l12, rescale = F)
# plot both circles with possible connections between them
plot.igraph(igraph12b, layout = l12, rescale = F)
Then, if you prefer it in visNetwork you can do that. Make sure you have an UPDATED version of visNetwork because the layout.norm with layoutMatrix capability is a recent addition:
visNetwork(nodes = rbind(nodes1, nodes2), edges = edges12) %>%
visIgraphLayout(layout="layout.norm", layoutMatrix = l12)
Of course, you'll want to add node colors to all of these.
Upvotes: 2