make_sankey: Make a Sankey diagram

View source: R/sankey.R

make_sankeyR Documentation

Make a Sankey diagram

Description

A Sankey diagram is a flow diagram in which the width of the lines is proportional to the rate of energy flow. Sankey diagrams are a helpful way to visualize energy flows in an energy conversion chain (ECC). This function takes a matrix description of an ECC and produces a Sankey diagram.

Usage

make_sankey(
  .sutmats = NULL,
  R = Recca::psut_cols$R,
  U = Recca::psut_cols$U,
  V = Recca::psut_cols$V,
  Y = Recca::psut_cols$Y,
  simplify_edges = TRUE,
  colour_string = NULL,
  name_col = "name",
  colour_col = "colour",
  sankey = Recca::sankey_cols$sankey,
  ...
)

Arguments

.sutmats

An optional wide-by-matrices data frame

R, U, V, Y

See Recca::psut_cols.

simplify_edges

A boolean which tells whether edges should be simplified. Applies to every row of .sutmats if .sutmats is specified.

colour_string

An optional Javascript string that defines colours for the Sankey diagram, appropriate for use by networkD3::sankeyNetwork(). Default is NULL, meaning that the default color palette should be used, namely networkD3::JS("d3.scaleOrdinal(d3.schemeCategory20);"). Can be a data frame with name_col and colour_col columns in which case create_sankey_colour_string() is called internally with colour_string, name_col, and colour_col as the arguments.

name_col, colour_col

The names of columns in colour_df for names of nodes and flows (name_col) and colours of nodes and flows (colour_col). Defaults are "name" and "colour", respectively.

sankey

See Recca::sankey_cols.

...

Arguments passed to networkD3::sankeyNetwork(), mostly for formatting purposes.

Details

At present, this function uses networkD3::sankeyNetwork() to draw the Sankey diagram.

If any of R, U, V, or Y is NA, NA is returned.

Note that there appears to be a colour bug in networkD3::sankeyNetwork() when a node name ends in a ".". Colours for those nodes does not render correctly.

Value

a Sankey diagram

Examples

library(dplyr)
library(magrittr)
library(networkD3)
library(tidyr)
# Default colours are likely to appear nearly random.
UKEnergy2000mats |>
  tidyr::pivot_wider(names_from = "matrix.name",
                     values_from = "matrix") |>
  make_sankey() |>
  extract2("Sankey") |>
  extract2(1)
# Create your own colour palette.
colour_df <- tibble::tribble(~name, ~colour,
                             "Resources [of Crude]", "gray",
                             "Crude dist.",          "gray",
                             "Oil fields",           "gray",
                             "Oil refineries",       "gray",
                             "Diesel dist.",         "gray",
                             "Power plants",         "gray",
                             "Elect. grid",          "gray",
                             "Resources [of NG]",    "gray",
                             "NG dist.",             "gray",
                             "Gas wells & proc.",    "gray",
                             "Petrol dist.",         "gray",
                             "Transport",            "gray",
                             "Residential",          "gray",
                             "Waste",                "gray",
                             "Crude",                "black",
                             "Crude [from Dist.]",   "black",
                             "Crude [from Fields]",  "black",
                             "Diesel",               "brown",
                             "Diesel [from Dist.]",  "brown",
                             "Diesel [from Fields]", "brown",
                             "Elect",                "yellow",
                             "Elect [from Grid]",    "yellow",
                             "NG",                   "lightblue",
                             "NG [from Dist.]",      "lightblue",
                             "NG [from Wells]",      "lightblue",
                             "Petrol",               "orange",
                             "Petrol [from Dist.]",  "orange")
colour_df
UKEnergy2000mats |>
  tidyr::pivot_wider(names_from = "matrix.name",
                     values_from = "matrix") |>
  make_sankey(colour_string = colour_df,
              # Arguments are passed to networkD3::sankeyNetwork()
              fontSize = 10,
              fontFamily = "Helvetica",
              units = "ktoe",
              width = 400, # pixels
              height = 400 # pixels
  ) |>
  extract2("Sankey") |>
  extract2(1)

MatthewHeun/Recca documentation built on Dec. 10, 2024, 10 p.m.