Skip to content

Instantly share code, notes, and snippets.

@tmasjc
Created December 4, 2024 15:50
Show Gist options
  • Save tmasjc/ca9afc03e9eb7e1e4449e9dd23273fd0 to your computer and use it in GitHub Desktop.
Save tmasjc/ca9afc03e9eb7e1e4449e9dd23273fd0 to your computer and use it in GitHub Desktop.
Transform data and draw a Sankey diagram.
library(tidyverse)
library(highcharter)
raw <- readr::read_rds("sample.rds")
weight = "XXXX" # replace with correct column
vpouch <- raw |>
select(-matches(weight)) |>
names() |>
(\(x)
(tibble(v1 = x, v2 = lead(x))))() |>
drop_na()
tally_func <- function(src_df, var_x, var_y) {
src_df |>
group_by(across(all_of(c(var_x, var_y)))) |>
tally(.data[[weight]]) |>
rename_with(~ c("from", "to", "weight"))
}
df <- vpouch |>
pmap(~ tally_func(..1, ..2, src_df = raw)) |>
bind_rows() |>
ungroup()
highchart2() |>
hc_chart(type = 'sankey') %>%
hc_add_series(data = df)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment