1

So I haven't found any clear solutions in the r documentation for the Sankey diagram, and hoping someone could help me! All I want to do is make the links the same color as the source node, and have the link darken when hovering above it. Here's my Sankey Diagram as it exists at the moment, unfortunately, I can't share the data as there are some confidentiality issues. At the bottom you'll find a link for the image of the plot that I have.

dt <- setDT(copy(dt_minors2016))
nodes <- dt[,unique(c(citizen,geo))]
sources <- match(dt[,citizen],nodes)-1
targets <- match(dt[,geo], nodes) -1
values <- dt[,V1]

fig <- plot_ly(
  type = "sankey",
  #default= 1000,
  domain = list(
    x =  c(0,1),
    y =  c(0,1)
  ),
  orientation = "h",
  valueformat = ".0f",
  valuesuffix = "Persons",
  
  node = list(
    label = nodes,
    # color = colors,
    pad = 15,
    thickness = 15,
    line = list(
      color = "black",
      width = 0.5
    )
  ),
  
  link = list(
    source = sources,
    target = targets,
    value =  values,
    color = 'rgba(0,255,255,0.4)'
  )
)
fig <- fig %>% layout(
  title = "UAM asylum seekers from top 5 origin countries to EU countries - 2016",
  font = list(
    size = 10
  ),
  xaxis = list(showgrid = F, zeroline = F),
  yaxis = list(showgrid = F, zeroline = F),
  hovermode = "x unified"
)

fig

https://i.stack.imgur.com/oAMh8.png

Armen Abagyan
  • 11
  • 1
  • 3
  • I have a solution - it's in python and I'm not inclined to recode in R. It's is plotly so approach will be same in R just slightly different language syntax. if you update question to accept python as well I can provide answer – Rob Raymond Oct 10 '21 at 08:57
  • Thanks so much for your response! Just edited the tags so should work now :) – Armen Abagyan Oct 11 '21 at 11:20
  • [Here](https://stackoverflow.com/a/68919892/9841389) you can find an example using plotly's R api. – ismirsehregal Nov 01 '21 at 13:47

1 Answers1

2
  • as per comments, solution provided in python not R
  • core to solution is setting color on both nodes and links using the imdex associated with name to select a color from a predefined color list
import pandas as pd
import numpy as np
import plotly.graph_objects as go
import plotly.express as px
import itertools

df = pd.DataFrame(
    itertools.product(
        ["AF", "SY", "IQ", "SO", "ER"], ["DE", "AT", "BG", "SE", "UK", "CH"]
    ),
    columns=["source", "target"],
).pipe(lambda d: d.assign(value=np.random.uniform(1, 10000, 1000)[:len(d)]))

nodes = np.unique(df[["source", "target"]], axis=None)
nodes = pd.Series(index=nodes, data=range(len(nodes)))

fig = go.Figure(
    go.Sankey(
        node={
            "label": nodes.index,
            "color": [
                px.colors.qualitative.Plotly[i % len(px.colors.qualitative.Plotly)]
                for i in nodes
            ],
        },
        link={
            "source": nodes.loc[df["source"]],
            "target": nodes.loc[df["target"]],
            "value": df["value"],
            "color": [
                px.colors.qualitative.Plotly[i % len(px.colors.qualitative.Plotly)]
                for i in nodes.loc[df["source"]]
            ],
        },
    )
)

fig

enter image description here

Rob Raymond
  • 29,118
  • 3
  • 14
  • 30