简体   繁体   English

R中的HighCharts Sankey图

[英]HighCharts Sankey Diagram in R

I'd like to create a sankey diagram using the highcharter library in R. Usually I'm just able to look at the javascript code for the plot and translate it for R, but for sankey plots I'm having some trouble. 我想使用R中的highcharter库创建一个sankey图。通常,我只能查看该图的javascript代码并将其转换为R,但是对于sankey图,我遇到了一些麻烦。 I'd like to just start by creating something like this: http://jsfiddle.net/highcharts/z2rL672w/3/ 我想从创建这样的东西开始: http : //jsfiddle.net/highcharts/z2rL672w/3/

Here's my attempt so far. 到目前为止,这是我的尝试。 I'm having trouble where to place the "keys" argument. 我在哪里放置“键”参数时遇到麻烦。

highchart() %>%
  hc_chart(type='sankey') %>%
  hc_add_series_list(
    list(
      keys=c('from', 'to', 'weight')
    ),
    list(
      data=list(
        list(
          from='AT',
          to='DE',
          weight=10
        ),
        list(
          from='DE',
          to='CH',
          weight=5
        ),
        list(
          from='DE',
          to='FI',
          weight=5
        )
      )
    )
  )

EDIT: 编辑:

I'm now trying the following. 我现在正在尝试以下方法。 Still having a bit of trouble 还是有点麻烦

library(highcharter)
library(tidyverse)
library(jsonlite)

dat <- data.frame(from=c('AT', 'DE', 'CH', 'DE'),
                   to=c('DE', 'CH', 'DE', 'FI'),
                   weight=c(10, 5, 15, 5)) %>%
  toJSON()

highchart() %>%
  hc_chart(type='sankey') %>%
  hc_series(dat)

I used the function hc_add_series (without keys) and it worked: 我使用了函数hc_add_series (不带键),它起作用了:

for the fisrt attempt: 对于第一次尝试:

highchart() %>%
  hc_chart(type = 'sankey') %>%
  hc_add_series(
      data = list(
        list(from = 'AT', to = 'DE', weight = 10),
        list(from = 'DE', to = 'CH', weight = 5),
        list(from = 'DE', to = 'FI', weight = 5))
      )

在此处输入图片说明

for the second attempt: 第二次尝试:

library(highcharter)
library(tidyverse)
library(jsonlite)

dat <- data.frame(from = c('AT', 'DE', 'CH', 'DE'),
                  to = c('DE', 'CH', 'DE', 'FI'),
                  weight = c(10, 5, 15, 5)) %>%
  toJSON()

highchart() %>%
  hc_chart(type = 'sankey') %>%
  hc_add_series(data = dat)

在此处输入图片说明

I hope that could help :) 我希望这可以帮助:)

edited note: 编辑笔记:

I use a development version 0.6.0 of highcharter, to install it please use: devtools::install_github("jbkunst/highcharter") 我使用highcharter的开发版本0.6.0,要安装它,请使用: devtools::install_github("jbkunst/highcharter")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM