Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Highcharter Sankey diagram with repeated "to" and "from" node names

I am trying to visualise migration data with a Sankey diagram, in which names of nodes will be repeated between the "from" and "to" columns of the data frame. Unfortunately, highcharter tries to use single nodes and makes the edges go back and forth:

# import and prepare the data
flows <- read.table("https://raw.githubusercontent.com/holtzy/data_to_viz/master/Example_dataset/13_AdjacencyDirectedWeighted.csv",
                    header = TRUE,
                    check.names = FALSE)
flows$from <- rownames(flows)
library(tidyr)
flows <- flows %>% 
  pivot_longer(-from, names_to = "to", values_to = "weight")
# visualise
library(highcharter)
hchart(flows, "sankey")

Sankey diagram with "to" and "from" nodes repeated

How would one force the nodes to be placed on two separate columns, while keeping the same colour for each area/continent?

I have used the workaround or renaming the "to" nodes so they don't share names (e.g. prepending "to " to each of them), but I would like to keep the same names and have the colours match.

# extra data preparation step for partial workaround
flows$to <- paste("to", flows$to)

Different names, and the colours don't match

like image 784
stragu Avatar asked Sep 15 '25 13:09

stragu


1 Answers

I had the same trouble and it was very frustrating. The only way that worked relatively well for me was, following your approach, generating white space before the names in the "to" column, like this:

data %>% data_to_sankey() %>% mutate(to = paste(" ", to)) %>% hchart(type = "sankey")

I hope this can help you.

Thank you!

like image 105
Juan Ignacio Fulponi Avatar answered Sep 18 '25 05:09

Juan Ignacio Fulponi