Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

283
Views
Create Sankey/ Alluvial diagram in which the links combine at the node

In all examples I can find for Sankey/ Alluvial diagrams I see the links come together at the node in such a way that the size of the node is the sum of all the links connecting to it. However, I would like to vizualize a matching procedure, in which 2 databases are matched, into 3 new datasets (A: the data from dataset 1, that could not be matched; B: the data that could be matched between the 2 datasets; and C: the data from dataset 2, that could not be matched).

If I draw a super simple version of this in paint, it looks something like this:

enter image description here

Is there a way to do this in R, python, or D3JS? Preferably in the R package networkD3 ot ggplot, but any software is acceptable.

In my real data, there will be multiple steps of matching and more than 2 datasets, that is why I want to implement this in R, python, or JS and not make an oneof version in Adobe.

Edit

Please, this is a labelled version of the plot, in which 75 from both A and B 'connect' together to D. So that A + B > C + D + E

enter image description here

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

  • you have stated python in question, hence answer in python
  • this really is all about data preparation. re-using this answer plotly sankey graph data formatting
  • create two data frames, one of 100 rows and a second one of 150 rows. These will have overlaps based on key has overlap values
  • find the counts of rows that are only in data frame / overlap
  • prepare nodes names and values. I don't see a way to fully meet your calculation. I've elected that target has correct value, however source is half of what actually flows
  • create figure
import pandas as pd
import plotly.graph_objects as go
import numpy as np

# two data sets with some overlap...
df1 = pd.DataFrame({"key":range(0,100)})
df2 = pd.DataFrame({"key":range(25, 175)})

# calculate overlap rows
o1 = df1["key"].isin(df2["key"]).value_counts()
o2 = df2["key"].isin(df1["key"]).value_counts()

# prep overlap rows into structure ready for sankey figure
df = pd.DataFrame([{"source":str(len(df1)), "target":"a" + str(o1[False]), "value":o1[False]},
              {"source":str(len(df1)), "target":"b" + str(o1[True]), "value":o1[True]/2},
              {"source":str(len(df2)), "target":"b" + str(o2[True]), "value":o2[True]/2},
              {"source":str(len(df2)), "target":"c" + str(o2[False]), "value":o2[False]},
             ])

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

go.Figure(
    go.Sankey(
        node={"label": nodes.index},
        link={
            "source": nodes.loc[df["source"]],
            "target": nodes.loc[df["target"]],
            "value": df["value"],
        },
    )
)

enter image description here

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!