简体   繁体   中英

R/Javascript: Collapsing and Expanding Networks

I am working with the R programming language.

I have the following graph.network data:

library(igraph)
library(visNetwork)

from <- c("Boss", "TeamA", "TeamA", "TeamA", "SubteamA1", "SubteamA1", "SubteamA1", "SubteamA2", "SubteamA2", "SubteamA2", "SubteamA3", "SubteamA3", "SubteamA3")
to <- c("TeamA", "SubteamA1", "SubteamA2", "SubteamA3", "employee1", "employee2", "employee3", "employee4", "employee5", "employee6", "employee7", "employee8", "employee9")
a1 = data_frame <- data.frame(from, to)


from <- c("Boss", "TeamB", "TeamB", "TeamB", "SubteamB1", "SubteamB1", "SubteamB1", "SubteamB2", "SubteamB2", "SubteamB2", "SubteamB3", "SubteamB3", "SubteamB3")
to <- c("TeamB", "SubteamB1", "SubteamB2", "SubteamB3", "employee10", "employee11", "employee12", "employee13", "employee14", "employee15", "employee16", "employee17", "employee18")
a2 = data_frame <- data.frame(from, to)


final = rbind(a1, a2)

I then made it into a graph.network and visualized it:

# Convert the data frame to an igraph object
g <- graph_from_data_frame(final, directed=FALSE)

# Plot the graph
plot(g)

# Optional visualization
visIgraph(g)

visIgraph(g) %>%
  visHierarchicalLayout(direction = "LR") %>%
  visInteraction(navigation = "zoom") %>%
  visInteraction(navigation = "drag") %>%
  visOptions(selectedBy = "to", 
             highlightNearest = TRUE, 
             nodesIdSelection = TRUE) 

在此处输入图像描述

My Question: I have been trying to find if there some way such that when you run the graph, it only shows one node on the screen (boss node) - and when you click on the boss node, it expands into 3 nodes (boss, team a, team b), and if you click on "team a", it expands into sub teams... but if you double click, it collapse back to the previous layer.

The closest thing I could find to this is here: https://github.com/datastorm-open/visNetwork/issues/307

But is there some easier way to do this in R/javascript? In the end, the final output should be a (standalone) HTML file that can be viewed offline.

Thanks!

Note:

You might try

  • install the chart layout feature from github:
devtools::install_github("timelyportfolio/networkD3@feature/d3.chart.layout")

which makes some layouts of { networkD3 } collapsible (see this SO post ). Example:

## devtools::install_github("timelyportfolio/networkD3@feature/d3.chart.layout")
library(networkD3)

hc <- hclust(dist(USArrests), "ave")

hierNetwork(as.treeNetwork(hc), 
            type = 'cluster.cartesian', 
            zoomable = TRUE,
            collapsible = TRUE
            )

An option could be using visOptions with the collapse argument:

: Custom option. Just a Boolean, or a named list. Collapse / Uncollapse nodes using double-click. In dev.

So this makes it possible to collapse when double-clicking on a node. You could change the shape to give it a different shape when it is collapsed. Here is some reproducible code:

library(igraph)
library(visNetwork)

visIgraph(g) %>%
  visInteraction(navigation = "zoom") %>%
  visInteraction(navigation = "drag") %>%
  visOptions(collapse = list(enabled = TRUE, keepCoord = TRUE, clusterOptions = list(shape = "circle"))) 

Created on 2023-01-30 with reprex v2.0.2

When clicking on your boss node:

在此处输入图像描述

Or for example on TeamA:

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM