0

I am working on making a sankey diagram in R, but having trouble with the LinkGroup code. I attach parts of my code which should explain the problem and the head of the data. I have a current plot without the legend of products, and I also attach what I eventually want R to do automatically with the LinkGroup function (right now I constructed the legend manually for illustration).

I currently have a sankey diagram with source and target (countries), with products ('major_prod') transferred between the source and target. I want to show the legend containing the different products transferred (beef, palm oil, wheat etc.)

Here is a reproducible example. The problem remains that the sankey has colour, but the legend of the product (Meat, palm oil, rice nuts etc.) does not show.

library(htmltools)
library(htmlwidgets)
library(magrittr)
library(networkD3)
library(tidyr)

Sankey <- data.frame("origin" = c('country A', 'country A', 'country A', 'country A', 'country B', 'country B', 'country C', 'country C'),
"destination" = c('country B', 'country B', 'country C', 'country D', 'country D', 'country D', 'country E', 'country E'),
'product' = c('Meat', 'Palm oil', 'Meat', 'Meat', 'Wheat', 'Other', 'Rice', 'Nuts'),
'number' = c(10, 10, 20, 30, 40, 35, 50,10)) 

nodes <- as.data.frame(union(unique(Sankey$origin), unique(Sankey$destination))) %>%
rename(Countries = 1)

Sankey$ID_major_Origin_EU <- match(Sankey$origin, nodes$Countries)-1 
Sankey$ID_major_Destination_EU <- match(Sankey$destination, nodes$Countries)-1

Sankey$ID_major_Origin_EU <- match(Sankey$origin, nodes$Countries)-1 
Sankey$ID_major_Destination_EU <- match(Sankey$destination, nodes$Countries)-1

head(Sankey)
nodes %<>% tidyr::separate(Countries, "Countries", sep = "_")

sankeyNetwork(Links = Sankey, 
          Nodes = nodes, 
          Source = "ID_major_Origin_EU", Target = "ID_major_Destination_EU", 
          Value = "number", NodeID = "Countries",
          units = "t dm", fontSize = 20, nodeWidth = 30,
          height = 600,
          #sinksRight = T,
          LinkGroup = c("product")) %>%
htmlwidgets::prependContent(htmltools::tags$h1("2013 - embodied HANPP"))

I want to show a legend containing the 'product' on the side of the sankey diagram but I cannot find a way. Any hints or solutions?

slamballais
  • 3,161
  • 3
  • 18
  • 29
  • 1
    It's not very clear what you're asking. Do you want to add a legend? Do you want to know how to use the LinkGroup argument properly? It would also be much easier for someone to help you if you provide a minimal reproducible example... meaning providing a small but representative sample of the data you're working with. – CJ Yetman Jun 05 '21 at 12:49
  • @cjyetman Thanks for your response! I have edited my original question to make it clearer. – Manan Bhan Jun 07 '21 at 07:53
  • I still don't quite understand.... your example does not produce a plot with a legend. And the `sankeyNetwork()` function does not have the capability to produce a legend by itself. So, is your question "how do I add a legend to the Sankey plot?"? – CJ Yetman Jun 07 '21 at 09:03
  • @cjyetman yes basically that is my question! sorry for running around in circles. the sankey does not seem to built for adding legends, although that is something I would like to do. – Manan Bhan Jun 08 '21 at 13:08

0 Answers0