R/Javascript:收缩和扩展网络

jxct1oxe  于 2023-02-01  发布在  Java
关注(0)|答案(2)|浏览(143)
    • bounty将在5天后过期**。回答此问题可获得+100的声誉奖励。stats_noob正在寻找来自声誉良好来源的答案:你好!我很想了解在R中解决这个问题的不同方法!最后,最终输出应该是一个(独立的)HTML文件,可以离线查看。注意:我不是在找一个闪闪发光的选择。非常感谢!

我正在使用R编程语言。
我有以下图表网络数据:

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)

然后我把它做成一个图网络,并把它可视化:

# 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)

    • 我的问题:**我一直在尝试寻找是否有某种方法,使当你运行图表时,它只在屏幕上显示一个节点(老板节点)-而当你点击老板节点时,它会扩展为3个节点(老板,团队a,团队b),如果你点击"团队a",它会扩展为子团队...但如果你双击,它会崩溃回到前一层。

我能找到的最接近这个的东西在这里:https://github.com/datastorm-open/visNetwork/issues/307
但是在R/javascript中有没有更简单的方法来做到这一点呢?最后,最终输出应该是一个(独立的)HTML文件,可以离线查看。
谢谢!
注:

dauxcl2d

dauxcl2d1#

你可以试试

  • 从github安装图表布局功能:
devtools::install_github("timelyportfolio/networkD3@feature/d3.chart.layout")

这使得{networkD3}的某些布局可折叠(请参见this SO post)。示例:

## 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
            )
hec6srdp

hec6srdp2#

一个选项可以是将visOptionscollapse参数一起使用:
:自定义选项。只是一个布尔值,或者一个命名列表。使用双击折叠/取消折叠节点。
这样就可以在双击一个节点时折叠它。你可以改变它的形状,使它在折叠时呈现不同的形状。下面是一些可重复的代码:

library(igraph)
library(visNetwork)

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

创建于2023年1月30日,使用reprex v2.0.2
当单击您的Boss节点时:

或者以TeamA为例:

1.是否可以删除每个节点上的“群集”标签?
您可以像这样添加label = FALSE

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

B组示例:

相关问题