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 isNULL
, meaning that the default color palette should be used, namelynetworkD3::JS("d3.scaleOrdinal(d3.schemeCategory20);")
. Can be a data frame withname_col
andcolour_col
columns in which casecreate_sankey_colour_string()
is called internally withcolour_string
,name_col
, andcolour_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.
Examples
library(dplyr)
library(magrittr)
#>
#> Attaching package: ‘magrittr’
#> The following object is masked from ‘package:tidyr’:
#>
#> extract
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
#> # A tibble: 27 × 2
#> name colour
#> <chr> <chr>
#> 1 Resources [of Crude] gray
#> 2 Crude dist. gray
#> 3 Oil fields gray
#> 4 Oil refineries gray
#> 5 Diesel dist. gray
#> 6 Power plants gray
#> 7 Elect. grid gray
#> 8 Resources [of NG] gray
#> 9 NG dist. gray
#> 10 Gas wells & proc. gray
#> # ℹ 17 more rows
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)