2

我正在创建图形结构

id    <- c(1,2,3,4,5,6,7,8,9)
label <- c("All", "Cat", "Dog", "Rice","Fish", "Bread","Rice","Fish", "Bread")

nodes <- data.frame(id, label)

edges <- data.frame(
from = c(1,1,2,2,2,3,3,3),
to = c(2,3,4,5,6,7,8,9)
  )



visNetwork(nodes, edges, width = "100%",height = "800px") %>%  visNodes(shape = "square") %>% 
  visEdges(arrows = "to") %>% 
  visInteraction(navigationButtons = TRUE)%>% 
  visHierarchicalLayout(levelSeparation = 200) %>% 
  visOptions(manipulation = TRUE)

期待它像这样出现。

预期产出

然而实际输出是这样的

实际输出

节点位置不正确,我无法手动移动节点,这很难解释。需要帮助根据上面的预期输出重新排列节点。

4

1 回答 1

2

您可以指定每个节点的级别以获得所需的方向。

library(visNetwork)
id    <- c(1,2,3,4,5,6,7,8,9)
label <- c("All", "Cat", "Dog", "Rice","Fish", "Bread","Rice","Fish", "Bread")

nodes <- data.frame(id, label, level = c( 1,2,2,3,3,3,3,3,3))

edges <- data.frame(
  from = c(1,1,2,2,2,3,3,3),
  to = c(2,3,4,5,6,7,8,9)
)

visNetwork(nodes, edges, width = "100%",height = "800px") %>%  visNodes(shape = "square") %>% 
  visEdges(arrows = "to") %>% 
  visInteraction(navigationButtons = TRUE)%>% 
  visHierarchicalLayout(levelSeparation = 200) %>% 
  visOptions(manipulation = TRUE)

在此处输入图像描述

于 2017-08-17T09:58:22.253 回答