Ali*_*ice 2 javascript csv json r d3.js
我正在尝试使用R中的D3气泡图来制作带有分组气泡颜色的气泡图表.
我已将index.html和flare.json文件从D3 上传到R中,并在运行时生成气泡图.但我不想手动更改此JSON代码以创建我自己的气泡和组(下面的标题显示了一组3个气泡组,其中包含不同组的名称).
{
"name": "flare",
"children": [
{
"name": "analytics",
"children": [
{
"name": "cluster",
"children": [
{"name": "AgglomerativeCluster", "size": 3938},
{"name": "CommunityStructure", "size": 3812},
{"name": "HierarchicalCluster", "size": 6714},
{"name": "MergeEdge", "size": 743}
]
},
{
"name": "graph",
"children": [
{"name": "BetweennessCentrality", "size": 3534},
{"name": "LinkDistance", "size": 5731},
{"name": "MaxFlowMinCut", "size": 7840},
{"name": "ShortestPaths", "size": 5914},
{"name": "SpanningTree", "size": 3416}
]
},
{
"name": "optimization",
"children": [
{"name": "AspectRatioBanker", "size": 7074}
]
}
]
Run Code Online (Sandbox Code Playgroud)
使用jsonlite包(从在线阅读可以处理更复杂的json结构)我已经转换为数据帧.
library(jsonlite)
fromJSON("flare.json",simplifyDateframe = FALSE)
Run Code Online (Sandbox Code Playgroud)
这没有请求的数据帧结构(示例).
$children[[10]]$children[[6]]$children[[10]]
$children[[10]]$children[[6]]$children[[10]]$name
[1] "OperatorSwitch"
$children[[10]]$children[[6]]$children[[10]]$size
[1] 2581
Run Code Online (Sandbox Code Playgroud)
这是与请求的数据帧结构(例子).
fromJSON("flare.json",simplifyDataFrame = TRUE)
Run Code Online (Sandbox Code Playgroud)
然而,它产生了一个长连接的数据列表,我一直试图解开这些数据以自动化我的数据.
Arrays, Colors, Dates, Displays, Filter, Geometry, heap, IEvaluable, IPredicate, IValueProxy, math, Maths, Orientation, palette, Property, Shapes, Sort, Stats, Strings, 8258, 10001, 8217, 12555, 2324, 10993, NA, 335, 383, 874, NA, 17705, 1486, NA, 5559, 19118, 6887, 6557, 22026, FibonacciHeap, HeapNode, 9354, 1233, DenseMatrix, IMatrix, SparseMatrix, 3165, 2815, 3366, ColorPalette, Palette, ShapePalette, SizePalette, 6367, 1229, 2059, 2291
Run Code Online (Sandbox Code Playgroud)
建议的解决方案......
FOR LOOPS(限时)
我已经考虑过编写多个for循环来重新构造JSON嵌套结构(我更强大,但我有一个截止日期,这可能需要一段时间).但我认为更多JSON savy的人可能会提供帮助.
CSV转换格式(不起作用)
我还尝试使用JSON将flare.json文件转换为CSV转换器,以生成测试是否可以将CSV中的内容直接更新为R所需的CSV格式,但这样做不起作用(即使添加了光斑).从jsonlite到JSON不自动化的json标头内容.
将flare.json从JSON转换为数据框或表的解决方案,以便我可以上传我的数据,包括名称,大小和组,以 转换回JSON以生成我自己的气泡图?
如果可能的话,在R中实现这一切会很棒,我认为这是不可能的,但很高兴听到其他建议.
我很难接受下一步该做什么.我通常处理R中的矩阵,因此处理JSON列表和数组不是我的强项.
这可能会为我们提供一些其他的思考方式.我会在代码中内联注释.你可以看到一个实例.
library(jsonlite)
library(dplyr)
flare_json <- rjson::fromJSON( ## rjson just works better on these for me
file = "http://bl.ocks.org/mbostock/raw/4063269/flare.json"
)
# let's have a look at the structure of flare.json
# listviewer htmlwidget might help us see what is happening
# devtools::install_github("timelyportfolio/listviewer")
# library(listviewer)
jsonedit(
paste0(
readLines("http://bl.ocks.org/mbostock/raw/4063269/flare.json")
,collapse=""
)
)
# the interesting thing about Mike Bostock's Bubble Chart example
# though is that the example removes the nested hierarchy
# with a JavaScript function called classes
#// Returns a flattened hierarchy containing all leaf nodes under the root.
#function classes(root) {
# var classes = [];
#
# function recurse(name, node) {
# if (node.children) node.children.forEach(function(child) { recurse(node.name, child); });
# else classes.push({packageName: name, className: node.name, value: node.size});
# }
#
# recurse(null, root);
# return {children: classes};
#}
# let's try to recreate this in R
classes <- function(root){
classes <- data.frame()
haschild <- function(node){
(!is.null(node) && "children" %in% names(node))
}
recurse <- function(name,node){
if(haschild(node)){
lapply(
1:length(node$children)
,function(n){
recurse(node$name,node$children[[n]])
}
)
} else {
classes <<- bind_rows(
classes,
data.frame(
"packageName"= name
,"className" = node[["name"]]
,"size" = node[["size"]]
,stringsAsFactors = F
)
)
}
}
recurse(root$name,root)
return(classes)
}
# now with a R flavor our class replica should work
flare_df <- classes(flare_json)
# so the example uses a data.frame with columns
# packageName, className, size
# and feeds that to bubble.nodes where bubble = d3.layout.pack
# fortunately Joe Cheng has already made a htmlwidget called bubbles
# https://github.com/jcheng5/bubbles
# that will produce a d3.layout.pack bubble chart
library(scales)
bubbles(
flare_df$size
,flare_df$className
,color = col_factor(
RColorBrewer::brewer.pal(9,"Set1")
,factor(flare_df$packageName)
)(flare_df$packageName)
,height = 600
,width = 960
)
# it's not perfect with things such as text sizing
# but it's a start
Run Code Online (Sandbox Code Playgroud)
如果你仍然认为你想要一个嵌套的d3 JSON层次结构,这里有一些代码.
# convert this to nested d3 json format
# this is example data provided in a comment to this post
df <- data.frame(
"overallgroup" = "Online"
,"primarygroup" = c(rep("Social Media",3),rep("Web",2))
,"datasource" = c("Facebook","Twitter","Youtube","Website","Secondary Website")
,"size" = c(10000,5000,200,10000,2500)
,stringsAsFactors = FALSE
)
# recommend using data.tree to ease our pain here
#devtools::install_github("gluc/data.tree")
library(data.tree)
# the much easier way
df$pathString <- apply(df[,1:3],MARGIN=1, function(x){paste0(x,collapse="/")})
root <- as.Node(df[,4:5])
# the harder manual way
root <- Node$new("root")
sapply(unique(df[,1]),root$AddChild)
apply(
df[,1:ncol(df)]
,MARGIN = 1
,function(row){
lapply(2:length(row),function(cellnum){
cell <- row[cellnum]
if( cellnum < ncol(df) ){ # assume last column is attribute
parent <- Reduce(function(x,y){x$Climb(y)},as.character(row[1:(cellnum-1)]),root)
if(is.null(parent$Climb(cell))){
cellnode <- parent$AddChild( cell )
}
} else{
cellnode <- Reduce(function(x,y){x$Climb(y)},as.character(row[1:(cellnum-1)]),root)
cellnode$Set( size = as.numeric(cell) )
}
})
}
)
# now we should be able to supply root to networkD3
# that expects a typical d3 nested JSON
#devtools::install_github("christophergandrud/networkD3")
library(networkD3)
treeNetwork( root$ToList(unname=TRUE) )
# or to get it in JSON
jsonlite::toJSON( root$ToList(unname=TRUE), auto_unbox=TRUE)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1567 次 |
| 最近记录: |