我希望使用这段数据创建一个简单的sankey图表,并特别绘制包装,以便活动ABC指向Resouce“R5”,“BVN”到“R1”和“IOP”到“R6”。我跟踪了帖子https://plot.ly/r/sankey-diagram/,但我无法用这些数据自动化scipt。如果你能帮助我在没有JSON的情况下帮助我,我将不胜感激。数据如下。
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