r visNetwork 节点位置问题

r visNetwork node position issue

我正在创建图形结构

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)

期待这样的表现

然而实际输出是这样的

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

您可以为每个节点指定级别以获得您想要的方向。

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)