R Highcharter Sankey节点列属性问题

时间:2018-06-14 14:54:45

标签: r highcharts sankey-diagram r-highcharter

我想使用highcharter包在R中绘制一个Sankey图。我在格式化时面临问题。这是一个例子。

# devtools::install_github("jbkunst/highcharter")
library(highcharter)

hc_dat <- data.frame(from = c("A", "A", "B"), 
                     to = c("C", "B", "C"), N = c(7, 5, 5))
highchart() %>%
  hc_add_series(data = hc_dat, type = "sankey", 
                hcaes(from = from, to = to, weight = N))

这会产生以下图片: Out-of-box

我希望B节点处于中间位置,以便更好地了解情节。所以我尝试在Highcharts系列中实现这个操作column的{​​{1}}属性:

nodes

这不会改变图片。我发现,原因如下:nodes_mapping <- list(list(id = "A", column = 0), list(id = "B", column = 1), list(id = "C", column = 2)) highchart() %>% hc_add_series(data = hc_dat, type = "sankey", nodes = nodes_mapping, hcaes(from = from, to = to, weight = N)) 使用highcharter转换jsonlite::toJSON个对象,它在JSON中提供了不必要的R,这会破坏Highcharts的行为。

[]

相同但使用jsonlite::toJSON(nodes_mapping) # [{"id":["A"],"column":[0]},{"id":["B"],"column":[1]},{"id":["C"],"column":[2]}] 代替"A"等将起作用。 JS中的证明就在jsfiddle

我尝试使用["A"]在JavaScript中嵌入JavaScript,但它不起作用:

htmlwidgets::JS

所以,我在这里被困住了。有谁知道如何在这种情况下让highchart() %>% hc_add_series(data = hc_dat, type = "sankey", nodes = JS('[{"id":"A","column":[0]},{"id":"B","column":[1]},{"id":"C","column":[2]}]'), hcaes(from = from, to = to, weight = N)) # empty chart highchart() %>% hc_add_series(data = hc_dat, type = "sankey", JS('nodes: [{"id":"A","column":[0]},{"id":"B","column":[1]},{"id":"C","column":[2]}]'), hcaes(from = from, to = to, weight = N)) # Error: inherits(mapping, "hcaes") is not TRUE highchart() %>% hc_add_series(data = hc_dat, type = "sankey", hcaes(from = from, to = to, weight = N), JS('nodes: [{"id":"A","column":[0]},{"id":"B","column":[1]},{"id":"C","column":[2]}]')) # Error: Column 4 must be named 考虑系列的属性?

2 个答案:

答案 0 :(得分:2)

A-> B和B-> C的Sankey可以通过重新定义基础数据来完成:

 hc_dat <- data.frame(from = c("A", "B"), 
                 to = c("B", "C"), N = c(7, 5))

类似地,您可以从A-> C定义节点

 hc_dat <- data.frame(from = c("A", "B", "A"), 
                 to = c("B", "C", "C"), N = c(5, 5, 7))

但是,这并不能很好地绘制情节。

答案 1 :(得分:0)

正如评论中已经提到的那样,您可能想给let s:git_path = substitute(system("git rev-parse --show-toplevel 2>/dev/null"), '\n', '', '') function! s:ag_git_root(query, ...) if type(a:query) != type('') return s:warn('Invalid query argument') endif let query = empty(a:query) ? '^(?=.)' : a:query let args = copy(a:000) let ag_opts = len(args) > 1 && type(args[0]) == s:TYPE.string ? remove(args, 0) : '' let command = ag_opts . ' ' . fzf#shellescape(query) . ' ' . s:git_path return call('fzf#vim#ag_raw', insert(args, command, 0)) endfunction command! -bang -nargs=* A \ call s:ag_git_root(<q-args>, <bang>0) command! -bang -nargs=? F \ call fzf#vim#files(s:git_path, <bang>0) silent! nmap <C-P> :F<CR> silent! nmap <C-F> :A<CR> 做个尝试。

以下是基于您提供的示例数据的示例。

SELECT * FROM lesson_registrations
WHERE 
(startTime BETWEEN '201806271159' AND '201806271359' ) 

enter image description here