Sankey diagram using Plotly

时光总嘲笑我的痴心妄想 提交于 2019-12-12 04:35:33

问题


I wish to create a simple sankey chart using this piece of data and specifically plotly package such that, activity ABC points to Resouce "R5", "BVN" to "R1" and "IOP" to "R6". I followed the plotly post https://plot.ly/r/sankey-diagram/, but I just am not able to automate the scipt with this data. I would appreciate if you can help me do it without JSON. Data is attached below.

library(plotly)
library(rjson)
json_file <-"https://raw.githubusercontent.com/plotly/plotly.js/master/test/image/mocks/sankey_energy.json"
json_data <- fromJSON(paste(readLines(json_file), collapse=""))

p <- plot_ly(
type = "sankey",
domain = c(
  x =  c(0,1),
  y =  c(0,1)
),
orientation = "h",
valueformat = ".0f",
valuesuffix = "TWh",

node = list(
  label = json_data$data[[1]]$node$label,
  color = json_data$data[[1]]$node$color,
  pad = 15,
  thickness = 15,
  line = list(
    color = "black",
    width = 0.5
  )
),

link = list(
  source = json_data$data[[1]]$link$source,
  target = json_data$data[[1]]$link$target,
  value =  json_data$data[[1]]$link$value,
  label =  json_data$data[[1]]$link$label
)
) %>% 
   layout(
title = "Energy forecast for 2050<br>Source: Department of Energy & Climate Change, Tom Counsell via <a href='https://bost.ocks.org/mike/sankey/'>Mike Bostock</a>",
font = list(
  size = 10
),
xaxis = list(showgrid = F, zeroline = F),
yaxis = list(showgrid = F, zeroline = F)
)

Activity    Resource    Value
ABC         R2          40
BVN         R4          53
NCB         R5          45
UNI         R1          22
IOP         R3          34
GAD         R6          54

来源:https://stackoverflow.com/questions/46683969/sankey-diagram-using-plotly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!