具有重组的 R 对角线网络(不仅是树的分离)

R diagonal network with regroupments (not only tree's separations)

我在 R 中发现了很棒的 networkD3 包,我们可以用它来做这种 htmlwidget。

 library('networkD3')
 library('data.tree')
 Relationships<- data.frame(Parent=c("earth","earth","forest","forest","ocean","ocean","ocean","ocean","fish","seaweed"),
                    Child=c("ocean","forest","tree","sasquatch","fish","seaweed","mantis shrimp","sea monster","fish&seaweed","fish&seaweed"))
 tree <- FromDataFrameNetwork(Relationships)
 tree <- ToListExplicit(tree, unname = TRUE)
 diagonalNetwork(tree)

但是,我想用红色做这种表示(重组而不是分离),但我在 R 中找不到任何方法,因为 diagonalNetwork 需要我们使用树(不重组) .

我想保持线性呈现。例如,我不想要那样的东西...

 simpleNetwork(Relationships, fontFamily = "sans-serif",fontSize=10)

你有什么解决办法吗?非常感谢!

也许桑基图就是您要找的东西?

library('networkD3')

Links <- read.table(header = T, stringsAsFactors = F, text = "
Parent  Child
earth   ocean
earth   forest
forest  tree
forest  sasquatch
ocean   fish
ocean   seaweed
ocean   'mantis shrimp'
ocean   'sea monster'
fish    fish&seaweed
seaweed fish&seaweed
")

Nodes <- data.frame(name = unique(c(Links$Parent, Links$Child)))
Links$Parent <- match(Links$Parent, Nodes$name) - 1
Links$Child <- match(Links$Child, Nodes$name) - 1
Links$Value <- 1

sankeyNetwork(Links = Links, Nodes = Nodes, Source = "Parent", Target = "Child", 
              Value = "Value", NodeID = "name")

使用不同的图形界面,您可以使用 visNetwork 包:

 library(visNetwork)
 nodes <- data.frame(id = 1:10,
                label = c("earth","ocean","forest","fish","seaweed","mantis shrimp","sea monster","tree","sasquatch", "fish seaweed"),
                level = c(1,2,2,3,3,3,3,3,3,4))
 edges <- data.frame(from = c(1,1,2,2,2,2,3,3,10,10),
                to = c(2,3,4,5,6,7,8,9,4,5))
 visNetwork(nodes, edges) %>% 
     visNodes() %>% 
     visHierarchicalLayout(direction = "LR", levelSeparation = 500)