2017-08-16 53 views
1

我創建圖形結構[R 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) 

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) 

期待它出現這樣。

Expected Output

但是實際的輸出是這樣的

Actual Output

的節點位置是不正確的,我不能用手移動節點,這使得它很難解釋。需要幫助根據上面的預期輸出重新排列節點。

+0

問題在於這樣一個事實:'cat'和'dog'連接到同一個'rice','fish','bread'節點。 – emilliman5

+0

@ emilliman5,你說得對,我有同樣的感覺。我已經用你的建議更新了我的問題。 –

回答

1

您可以指定每個節點的級別以獲取所需的方向。

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) 

enter image description here

+0

不錯,謝謝:)。 –