1

Data Frame

df <- data.frame(
  "Level" = c(0,1,2,2,1,2)
  ,"Name" = c("A","B","C","D","E","F")
  ,"Value" = 1:6
  ,stringsAsFactors = FALSE
)

If level of next row is greater than the present row then it is a child of present row.

For example the above data frame is to be converted into a chart as shown below

Diagonal Network

**Diagonal Network**

What I have tried

1)Converted data frame to csv(Data.csv),then converted csv to JSON using jsonlite and thereafter passed it as a list to diagonalnetwork function.Please refer below for same and update the Path

library(networkD3)
library(jsonlite)
setwd(Path)
df <- data.frame(
  "Level" = c(0,1,2,2,1,2)
  ,"Name" = c("A","B","C","D","E","F")
  ,"Value" = 1:6
  ,stringsAsFactors = FALSE)

write.csv(df, file =Path, row.names = FALSE)
data1DF<-read.csv("Data.csv", header=TRUE, sep=",")
myjson<-jsonlite::toJSON(data1DF)
write(myjson, "data.json")
Flare <- jsonlite::fromJSON("data.json", simplifyDataFrame =FALSE)
diagonalNetwork (List= Flare, fontSize = 10, opacity = 0.9, margin=0)

2)Converted data frame to JSON using data.tree package and then pass as list to diagonalnetwork function

library(data.tree)
library(networkD3)
df <- data.frame("Level" = c(0,1,2,2,1,2)
      ,"Name" = c("A","B","C","D","E","F")
      ,"Value" = 1:6
      ,stringsAsFactors = FALSE)

 df$pathString <- paste("root2", df$Level, df$Name, df$Value, sep="/")
 root2 <-as.Node(df[,-c(1, 2)])
 root2 <-as.list(root2)
 diagonalNetwork (List= root2, fontSize = 10, opacity = 0.9, margin=0)

But both my attempts didn't provide the desired result.

What I really need

A solution to generate diagonal network chart from a data frame of thousands of row with hierarchy similar to our data frame. No constraints on the orientation of chart whether top down or left right,all i need is my data frame in proper hierarchy. Ultimately this chart will be used in R shiny based application.Kindly provide solution accordingly.

alessandrio
  • 4,282
  • 2
  • 29
  • 40
Tapan
  • 11
  • 2
  • Welcome to SO. What's `Data.csv`? Where's `diagonalNetwork` from? Please edit your post and make it a [minimal reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example#answer-5963610) as kindly asked by the R-tag-info (hover over the tag). – lukeA May 31 '17 at 12:45
  • @lukeA Thanks for your input.I have edited it,can you pls provide some solution. – Tapan Jun 02 '17 at 01:41

1 Answers1

4

This works:

library(data.tree)
library(networkD3)
df <- data.frame(from = c('A', 'B', 'B', 'A', 'E'), 
                 to = c('B', 'C', 'D', 'E', 'F'), 
                 stringsAsFactors = FALSE)
root2 <- data.tree::FromDataFrameNetwork(df)

root2 <- as.list(root2, mode = 'explicit', unname = TRUE)

diagonalNetwork(List = root2, fontSize = 10, opacity = 0.9, margin = 0)
CJ Yetman
  • 8,373
  • 2
  • 24
  • 56
Christoph Glur
  • 1,224
  • 6
  • 10