17  Sankey chart

In this section, we’re going to create a Sankey chart .

A Sankey chart is a flow diagram where the width of the connecting lines (links) is proportional to the quantity of the flow. It is used to visualize the movement of resources, energy, or data between different stages or categories (nodes).

In Supply Chain, it is a very useful way to display a flow of products.

Let’s start by uploading the libraries we will use : tidyverse for the ETL, and this time the new library networkD3 for the charts.

# ETL
library(tidyverse)

# Charts
library(networkD3)

We will use a demo data frame to create our chart, downloaded from the URL below :

# upload dataset

# Define the URL of the raw CSV file
url <- "https://raw.githubusercontent.com/nguyennico/chart_practice/main/Distribution_Network_data.csv"

# Read the CSV file from the URL
initial_data <- read.csv(url)

glimpse(initial_data)
Rows: 15
Columns: 5
$ distribution_center <chr> "Jakarta", "Jakarta", "Jakarta", "Jakarta", "Jakar…
$ channel_type        <chr> "Wholesaler", "Wholesaler", "Wholesaler", "Wholesa…
$ customer_name       <chr> "Distributor 1", "Distributor 1", "Distributor 1",…
$ product             <chr> "Product A", "Product B", "Product C", "Product A"…
$ sales_qty           <int> 1000, 2000, 800, 3200, 1700, 1000, 400, 900, 2000,…

It’s a data frame with 5 variables :

We will display the flow of products, from the Distribution Centers to the Customers (and the type of channel they are related to), based on the sales quantity.

A Sankey chart is created through 3 steps :

19 Create Nodes

And finally we create some nodes.

Nodes are unique combinations of Parents x Child from the Links data frame.

# create the Nodes dataframe based on the Links df
Nodes <- data.frame(name = unique(c(Links$Parent, Links$Child)))

20 Display Sankey chart

Here we are!

Now we can display our sankey chart, using the function sankeyNetwork() .

This function uses as inputs the 2 data frames we previously created : Links and Nodes.

Before this, we also create 2 additional variables in the data frame Links : source and target.

  • It’s to indicate the relation between the Parent and Child of the Links data frame with the names in the Nodes data frame. We use the function match() .

    • The match() function returns a vector of the positions (indices) of the first match of elements from the first argument (x) within the second argument
  • those 2 new variables from the Links data frame are also 2 inputs of the function sankeyNetwork() .

# add the IDs to the Links df
Links$source <- match(Links$Parent, Nodes$name) - 1
Links$target <- match(Links$Child, Nodes$name) - 1
    
    
#---------------------------------------------------------
# (d3) Plot. Sankey Products
# chart Sankey
    sankeyNetwork(
      Links = Links, 
      Nodes = Nodes, 
      Source = "source",
      Target = "target", 
      Value = "sales_qty", 
      NodeID = "name",
      units = "units", 
      fontSize = 12, 
      nodeWidth = 30)
Links is a tbl_df. Converting to a plain data frame.

Through this Sankey chart we can visualize the movement of the flows from the Distribution Center located in Jakarta :

  • mainly through a Wholesaler channel, with some relatively equal Distributors (4 in total).

  • some Products are more distributed by some wholesalers than others (for example, the Product C is mainly distributed by the Distributor 3).