This document showcases the plot_sankey() and prep_sankey() functions for producing Sankeys in R. These are basically convenience functions for the networkD3 package, where all you have to do is specify which columns contain the ‘nodes’ (i.e. country, trader), and which column to use to scale the sankey (i.e. value or volume).

These functions are stored in the following file:

source(here::here("helpers/plot_sankey.R"))

Here we use an example of EU imports of cocoa from Cote d’Ivoire.

plot_sankey()

This function plots a sankey, where each node is given a random colour.

# plot Sankey, random colours
plot_sankey(
  data = data_sbst,
  node1_col = "SENDING_COUNTRY",
  node2_col = "TRADER",
  node3_col = "COUNTRY",
  value = "NET_MASS_KG",
  max_num_groups = 10
)

prep_sankey()

This function prepares the data for a sankey, the user can then specify additional parameters to the `sankeyNetwork() function, to create different effects.

Here, I use a single colour per node type, and change the font to Merriweather.

# Create a list of objects required for plotting
# ... Specify the source of the data, the different columns to aggregate by
# ... and value (the column to aggregate)
res <- prep_sankey(
  data = data_sbst,
  node1_col = "SENDING_COUNTRY",
  node2_col = "TRADER",
  node3_col = "COUNTRY",
  value = "NET_MASS_KG",
  max_num_groups = 10
)


# Specify the colours to use per node type
my_colors <-
  'd3.scaleOrdinal() .domain(["SENDING_COUNTRY", "TRADER", "COUNTRY"]) .range(["#6BE4CC", "#639DE6", "#8C63E6"])'


# Plot the Sankey
my_sankey <- 
  sankeyNetwork(
    Links = res$Links,
    Nodes = res$Nodes,
    Source = 'SOURCE_ID',
    Target = 'TARGET_ID',
    Value = 'VALUE',
    NodeID = 'NAME',
    fontFamily = "Merriweather",
    fontSize = 16,
    colourScale = my_colors,
    NodeGroup = "GROUP"
  )
my_sankey

Export .html

These plots can be exported as html files by adapting the following code:

saveNetwork(network = my_sankey,
            file = "eu_cocoa",
            selfcontained = TRUE)