Collapsible tree in R
This collapsible tree looks really cool. My approach here is to first, create a graph using igraph
. I was hoping there was already a function to convert an igraph to json, however, it looks like that is an issue on github that hasn't been implemented. So, here is a simple function to do that. Then, you can just plug the resulting data into the linked source and you have a collapsible tree.
## Read your datadat <- read.table(text="ID Car Bus Train Feedback_Car Feedback_Bus Feedback_Train23433 Yes Yes Yes Toyota GreyHound Amtrak", header=TRUE)## Make an edgelist from your dataedges <- rbind(cbind(dat$ID, names(dat)[2:4]), cbind(names(dat)[2:4], as.vector(t(dat[5:7]))))## Convert to a graph data structurelibrary(igraph)g <- graph_from_edgelist(edges)## This is the non-interactive versionplot(g, layout=layout.reingold.tilford(g, root='23433'))
## Recursive function to make a list of nodes to be parsed by toJSON## call it with 'node' as the root node (here '23433')f <- function(g, node, size=1000) { n <- neighbors(g, node, mode='out') if (length(n) == 0) return( list(name=node, size=size) ) children <- lapply(n$name, function(x) f(g, x, size)) list(name=node, children=children)}## Convert to jsonlibrary(jsonlite)json <- toJSON(f(g, '23433'), auto_unbox = TRUE)## I made a directory collapsible to store the index.html from the linked## site, as well as this data## For completeness, you should be able to run this to see the interactive results,## But, of course, this is creating files on your boxdir.create('collapsible')writeLines(json, 'collapsible/data.json')## Download the index.htmldownload.file("https://gist.githubusercontent.com/mbostock/4339083/raw/0d003e5ea1686dd6e79562b37f8c7afca287d9a2/index.html", "collapsible/index.html", method='curl')## Replace with the correct datatxt <- readLines('collapsible/index.html')txt[grepl("^d3.json", txt)] <- "d3.json('data.json', function(error, flare) {"writeLines(txt, 'collapsible/index.html')## Open in broweserbrowseURL(paste0('file://', normalizePath('collapsible/index.html')))
The results can also be seen here.
I read the csv and make the node JSON structure like below:
d3.csv("my.csv", function(error, data) { var map1 = [] data.reduce(function(map, node) { map1.push(node) return node; }, {}); root = {}; root.name = map1[0].ID; root.children = []; var car = { name: "Car", children: [{ name: map1[0].Feedback_Car, children: [] }] }; root.children.push(car); var bus = { name: "Bus", children: [{ name: map1[0].Feedback_Bus, children: [] }] }; root.children.push(bus); var train = { name: "Bus", children: [{ name: map1[0].Feedback_Train, children: [] }] }; root.children.push(train);});
Working code here
Hope this helps!
I apologize for being so late. I think that you are looking for a solution in R, and not a solution that forces you to use outside code. Take advantage of the k3d3 package. https://github.com/kaseyriver11/k3d3Here is what want:
library(k3d3)library(RJSONIO)library(stringr)type <- c("Car", "Car", "Truck", "Truck", "Bus", "Bus")name <- c("Chevy", "Ford", "Chevy", "Ford", "Greyhound", "Holiday Express")size <- c(rep(3840,6))data <- data.frame(type, name, size)makeList<-function(x){ if(ncol(x)>2){ listSplit<-split(x[-1],x[1],drop=T) lapply(names(listSplit),function(y){list(name=y,children=makeList(listSplit[[y]]))}) }else{ lapply(seq(nrow(x[1])),function(y){list(name=x[,1][y],Percentage=x[,2][y])}) }}jsonOut<-toJSON(list(name="23433",children=makeList(data)))jsonOut2 <- str_replace_all(jsonOut, "[\r\n]" , "")CTR(jsonOut2)