antuki
antuki

Reputation: 236

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

I've discovered the wonderful networkD3 package in R with which we can do this kind of 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)

enter image description here

However, i would like to do this kind of representation (regroupments instead of separation) in red and I don't find any way to do it in R because diagonalNetwork needs that we work with a tree (no regroupment).

I would like to keep the linear presentation. For exemple I do NOT want something like that...

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

enter image description here

Do you have any solution for me ? Thanks a lot !

Upvotes: 2

Views: 907

Answers (2)

AQLT
AQLT

Reputation: 305

With a different graphical interface you could use the visNetwork package :

 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)

enter image description here

Upvotes: 1

CJ Yetman
CJ Yetman

Reputation: 8848

maybe a sankey plot is what you're looking for?

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

enter image description here

Upvotes: 2

Related Questions