Search code examples
rplotlysankey-diagram

Isolate path in Sankey Diagram using r-plotly


The Sankey plot I created has lots of nodes and links therefore I would like to isolate the path starting from the first source node as a separate plot using plotly in R. Using the example below from the plotly documentation how would I isolate the path that Gas reserves followed? I tried creating a new column that has the absolute source and filtering my data frame but when I do that the paths are broken i.e. Step 2 is aligned with Step 1. Basically what I would like to do is filter the sankey plot by each starting category. Any help is appreciated.

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=""))

fig <- plot_ly(
    type = "sankey",
    domain = list(
      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
    )
  ) 
fig <- fig %>% 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)
)

fig 

Solution

  • The easiest way is to indeed filter the data to only the desired starting node.