sankey_build_data: Prepare data to be input into the networkD3 sankeyNetwork...

View source: R/sankey.R

sankey_build_dataR Documentation

Prepare data to be input into the networkD3 sankeyNetwork function.

Description

Prepare data to be input into the networkD3 sankeyNetwork function.

Usage

sankey_build_data(data, colour_data)

Arguments

data

A tibble or dataframe with all categorical columns apart from the final column, which is numeric. Required input.

colour_data

A tibble or dataframe with variable 1 called name containing relevant categorical values and variable 2 called colour and containing hex codes (or colour names). Required input.

Value

A vector of labels.

Examples

library(dplyr)

plot_data <- tibble::tribble(
~state1, ~state2, ~value,
"Trees", "Grass", 5,
"Trees", "Wetland", 1,
"Grass", "Trees", 7,
"Grass", "Wetland", 1,
"Wetland", "Trees", 1,
"Wetland", "Grass", 2)

pal_data <- tibble::tribble(
  ~name, ~colour,
  "Trees", "purple",
  "Grass", "green",
  "Wetland", "brown")

sankey_data <- sankey_build_data(
  data = plot_data,
  colour_data = pal_data)

units <- "ha"

networkD3::sankeyNetwork(Links = sankey_data$links, Nodes = sankey_data$nodes,
                         Source = "source_id", Target = "target_id", Value = "value",
                         NodeID = "name", colourScale = sankey_data$colour_scale,
                         fontSize = 14, fontFamily = "Arial", units = units)

StatisticsNZ/er.helpers documentation built on Oct. 2, 2023, 7:24 a.m.