Ezr*_*son 10 tree json r d3.js
我对这篇关于R中可折叠树的文章感兴趣
http://bl.ocks.org/mbostock/4339083
我试图使用像这样的玩具数据集重现相同的例子
ID Car Bus Train Feedback_Car Feedback_Bus Feedback_Train
23433 Yes Yes Yes Toyota GreyHound Amtrak
Run Code Online (Sandbox Code Playgroud)
哪个可以表示为可折叠树,如下所示
我想知道是否有人可以帮助我使用上面的玩具数据集重现这个概念(可折叠树),这个例子将让我知道不同的组件是如何工作的,例如格式化R等中的JSON数据......并作为一个初始点.提前致谢.
这个可折叠的树看起来很酷.我的方法是首先使用创建图形igraph.我希望有已经到IGRAPH转换成JSON的功能,但是,它看起来就像是一个问题尚未落实在GitHub上.所以,这是一个简单的功能.然后,您可以将结果数据插入到链接的源中,并且您有一个可折叠的树.
## Read your data
dat <- read.table(text="ID Car Bus Train Feedback_Car Feedback_Bus Feedback_Train
23433 Yes Yes Yes Toyota GreyHound Amtrak", header=TRUE)
## Make an edgelist from your data
edges <- rbind(cbind(dat$ID, names(dat)[2:4]),
cbind(names(dat)[2:4], as.vector(t(dat[5:7]))))
## Convert to a graph data structure
library(igraph)
g <- graph_from_edgelist(edges)
## This is the non-interactive version
plot(g, layout=layout.reingold.tilford(g, root='23433'))
Run Code Online (Sandbox Code Playgroud)
## 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 json
library(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 box
dir.create('collapsible')
writeLines(json, 'collapsible/data.json')
## Download the index.html
download.file("https://gist.githubusercontent.com/mbostock/4339083/raw/0d003e5ea1686dd6e79562b37f8c7afca287d9a2/index.html", "collapsible/index.html", method='curl')
## Replace with the correct data
txt <- readLines('collapsible/index.html')
txt[grepl("^d3.json", txt)] <- "d3.json('data.json', function(error, flare) {"
writeLines(txt, 'collapsible/index.html')
## Open in broweser
browseURL(paste0('file://', normalizePath('collapsible/index.html')))
Run Code Online (Sandbox Code Playgroud)
结果也可以在这里看到.
您可以使用 data.tree 包将数据转换为 JSON,也可以使用 networkD3 包:
\n\ndat <- read.table(text="ID Car Bus Train Feedback_Car Feedback_Bus Feedback_Train\n23433 Yes Yes Yes Toyota GreyHound Amtrak", header=TRUE)\n\n## Make an edgelist from your data\nedges <- rbind(cbind(dat$ID, names(dat)[2:4]),\n cbind(names(dat)[2:4], as.vector(t(dat[5:7]))))\n\nlibrary(data.tree)\ntree <- FromDataFrameNetwork(as.data.frame(edges))\n\ntree\nRun Code Online (Sandbox Code Playgroud)\n\n将打印如下:
\n\n levelName\n1 23433 \n2 \xc2\xa6--Car \n3 \xc2\xa6 \xc2\xb0--Toyota \n4 \xc2\xa6--Bus \n5 \xc2\xa6 \xc2\xb0--GreyHound\n6 \xc2\xb0--Train \n7 \xc2\xb0--Amtrak \nRun Code Online (Sandbox Code Playgroud)\n\n现在,使用树结构与 networkD3 进行绘图:
\n\nlol <- ToListExplicit(tree, unname = TRUE)\n\nlibrary(networkD3)\n\ndiagonalNetwork(lol)\nRun Code Online (Sandbox Code Playgroud)\n\n不幸的是,它还不支持可折叠树。但在这儿是一个如何使用 Shiny 获得你想要的东西的例子。为了将数据转换为正确的 JSON 格式,只需执行以下操作:
\n\nlibrary(jsonlite)\njson <- toJSON(lol)\nRun Code Online (Sandbox Code Playgroud)\n