3

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

stragu
  • 1,051
  • 9
  • 15

1 Answers1

1

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!

  • Hey, struggeling with the same problems :/ Have you managed to set the same color for all flows? Check out the link for more clarity. https://imgur.com/a/VBBruiG – alex Apr 18 '23 at 11:28