I am working on network data and want to visualize it using sankeyNetwork()
from the networkd3 package in r.
In my data frame, I have a source node and a target node, value, and a grouping column group.
- How can I make the sankey plot without coloring the nodes (both source and target with no color)?
- 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")