0

I am working on network data and want to visualize it using sankeyNetwork() from the package in . In my data frame, I have a source node and a target node, value, and a grouping column group.

  1. How can I make the sankey plot without coloring the nodes (both source and target with no color)?
  2. I want to color the link lines by a variable group LinkGroup = "group" and I want to know how to make a legend to show that the different colors of the links are actually representing the group variable.

here is the code and the data frame...

# Libraries
library(dplyr)
library(tidyr)
library(tibble)
library(networkD3)

# Load dataset from github

data <- read.table("https://raw.githubusercontent.com/holtzy/data_to_viz/master/Example_dataset/13_AdjacencyDirectedWeighted.csv", header=TRUE) %>%
  rownames_to_column() %>%
  gather(key = "key", value = "value", -rowname) %>%
  filter(value > 0) %>% 
  rename(source = rowname,  target =key) %>%
  select("source", "target", "value") %>%
  mutate(group = c(rep("A", 10), rep("B",7), rep("C", 8), rep("D", 10)))
nodes <- data.frame(name=c(as.character(data$source), as.character(data$target)) %>% unique())

data$IDsource=match(data$source, nodes$name)-1 
data$IDtarget=match(data$target, nodes$name)-1

# Make the Network
sankeyNetwork(Links = data,
              Nodes = nodes,
              Source = "IDsource",
              Target = "IDtarget",
              Value = "value",
              NodeID = "name", 
              sinksRight=FALSE,
              nodeWidth=40,
              fontSize=13,
              nodePadding=20,
              LinkGroup = "group")
CJ Yetman
  • 8,373
  • 2
  • 24
  • 56
adR
  • 305
  • 4
  • 14

1 Answers1

0

You can continue to let the link nodes be given automatically chosen colors based on their group, and effectively turn off coloring of the nodes with the NodeGroup = NULL option/argument

sankeyNetwork(Links = data,
              Nodes = nodes,
              Source = "IDsource",
              Target = "IDtarget",
              Value = "value",
              NodeID = "name", 
              sinksRight=FALSE,
              nodeWidth=40,
              fontSize=13,
              nodePadding=20,
              LinkGroup = 'group',
              NodeGroup = NULL)

enter image description here


networkD3 does not have any built-in legend creation options, however, if you want to make a legend yourself, you'll probably want to use a consistent set of colors so that you can use them with whatever you use to make a legend. You can do that like this...

colourScale <- 
  'd3.scaleOrdinal()
     .domain(["A", "B", "C", "D"])
     .range(["red", "blue", "green", "yellow"])'

sankeyNetwork(Links = data,
              Nodes = nodes,
              Source = "IDsource",
              Target = "IDtarget",
              Value = "value",
              NodeID = "name", 
              sinksRight=FALSE,
              nodeWidth=40,
              fontSize=13,
              nodePadding=20,
              LinkGroup = 'group',
              NodeGroup = NULL,
              colourScale = colourScale)

enter image description here

CJ Yetman
  • 8,373
  • 2
  • 24
  • 56