marielle marielle - 11 months ago 82
R Question

Create hierarchical network

There is a way in R to create a hierarchical network like this?

enter image description here

That is, before it generates a hub of 5 nodes, to which are attached other 4 hubs, and so on until you get the general network.

enter image description here

I think I can use the

igraph function, but I don't know how to add hubs iteratively.
This is what I did.

net <- make_tree(n = 5, children = 4, mode = "undirected")

enter image description here

Thank you

Answer Source

Not sure how elegant this is. It would take more work to get the plot to look like yours but I think the graph is correct.


#' This adds the gadd graph to the main graph, g, and wires all of its vertices
#' to the central vertex of g
attach_to_center <- function(g, gadd) {
  g <- g + gadd + edges(as.vector(rbind(1, gorder(g) + 1:gorder(gadd))))

nIter <- 2
nChild <- 4

# The initial graph
g <- make_empty_graph(5, directed = FALSE) + edges(1,2,1,3,1,4,1,5,2,3,3,4,4,5,5,2)

for (j in 1:nIter) {
  g0 <- g
  for (i in 1:nChild) {
    g <- attach_to_center(g, g0)

plot(g, vertex.label = NA)

It seems quick enough (although plotting gets out of hand for more than 3 or 4 iterations).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download