如何为源节点和目标节点制作没有颜色的 sankyplots 以及如何做图例以显示链接组的不同颜色
how to make sankyplots with no color for source and target node and how to do legend to show the different colors of linkgroup
我正在处理网络数据并希望使用 networkd3 package in r 中的 sankeyNetwork()
将其可视化。
在我的数据框中,我有一个源节点和一个目标节点、值和一个分组列组。
- 如何在不给节点着色的情况下制作桑基图(源节点和目标节点都没有颜色)?
- 我想用变量组
LinkGroup = "group"
给 link 行着色,我想知道如何制作图例来显示 link 的不同颜色是实际上代表组变量。
这是代码和数据框...
# 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")
您可以继续让 link 节点根据其组自动选择颜色,并使用 NodeGroup = NULL
option/argument[=16 有效地关闭节点的着色=]
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)
networkD3
没有任何内置图例创建选项,但是,如果您想自己制作图例,您可能需要使用一组一致的颜色,以便您可以使用它们无论你用什么来创造传奇。你可以这样做...
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)
我正在处理网络数据并希望使用 networkd3 package in r 中的 sankeyNetwork()
将其可视化。
在我的数据框中,我有一个源节点和一个目标节点、值和一个分组列组。
- 如何在不给节点着色的情况下制作桑基图(源节点和目标节点都没有颜色)?
- 我想用变量组
LinkGroup = "group"
给 link 行着色,我想知道如何制作图例来显示 link 的不同颜色是实际上代表组变量。
这是代码和数据框...
# 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")
您可以继续让 link 节点根据其组自动选择颜色,并使用 NodeGroup = NULL
option/argument[=16 有效地关闭节点的着色=]
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)
networkD3
没有任何内置图例创建选项,但是,如果您想自己制作图例,您可能需要使用一组一致的颜色,以便您可以使用它们无论你用什么来创造传奇。你可以这样做...
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)