覆盖默认的 networkD3 图属性
Override default networkD3 graph properties
我正在尝试使用 networkd3 package in r 中的 sankeyNetwork()
设置自定义配色方案。这是我为 sankeyNetwork()
...
的 colourScale
参数设置的自定义颜色代码
# Give a color for each group:
my_color <-
'd3.scaleOrdinal()
.domain([
"Piped Water",
"Dug well protected",
"Tube well, borehole",
"Spring protected",
"Rainwater",
"Dug well unprotected",
"Spring unprotected",
"Tanker truck / cart",
"Surface water",
"Other",
"node_color"
])
.range([
"#2D20E1",
"#ABA7ED",
"#8726BE",
"#12B3F3",
"#6178e8",
"#F6F614",
"#BDBD07",
"#F4710E",
"#EEB56E",
"#6F3609"
])'
但是,此代码中设置的自定义颜色并没有像我预期的那样出现在输出桑基图中。
您可能 运行 遇到了 D3/JavaScript 仅比较 LinkGroup 值的第一个单词的问题,即忽略第一个 space 之后的所有内容。您可以通过在链接数据框中和 my_color
.
中的 JavaScript 中将 space 替换为其他内容(例如“_”)来解决此问题
这是一个完整的可重现示例...
library(networkD3)
nodes <- read.table(header = TRUE, text = '
name node_group
"Node 0" "node_color"
"Node 1" "node_color"
"Node 2" "node_color"
"Node 3" "node_color"
"Node 4" "node_color"
"Node 5" "node_color"
"Node 6" "node_color"
"Node 7" "node_color"
"Node 8" "node_color"
"Node 9" "node_color"
"Node 10" "node_color"
')
links <- read.table(header = TRUE, text = '
source target value link_group
0 10 1 "Piped Water"
1 10 1 "Dug well protected"
2 10 1 "Tube well, borehole"
3 10 1 "Spring protected"
4 10 1 "Rainwater"
5 10 1 "Dug well unprotected"
6 10 1 "Spring unprotected"
7 10 1 "Tanker truck / cart"
8 10 1 "Surface water"
9 10 1 "Other"
')
links$link_group <- gsub(" ", "_", links$link_group)
my_color <- paste0(
'd3.scaleOrdinal()
.domain(["',
paste0(links$link_group, collapse = '", "'),
'"])
.range([
"#F00", // red
"#0F0", // green
"#00F", // blue
"#FF0", // yellow
"#71A", // purple
"#EEE", // off-white
"#666", // grey
"#000", // black
"#520", // brown
"#FA0", // orange
"#AAA"
])')
sankeyNetwork(
Links = links,
Nodes = nodes,
Source = "source",
Target = "target",
Value = "value",
NodeID = "name",
NodeGroup = "node_group",
LinkGroup = "link_group",
colourScale = my_color
)
我正在尝试使用 networkd3 package in r 中的 sankeyNetwork()
设置自定义配色方案。这是我为 sankeyNetwork()
...
colourScale
参数设置的自定义颜色代码
# Give a color for each group:
my_color <-
'd3.scaleOrdinal()
.domain([
"Piped Water",
"Dug well protected",
"Tube well, borehole",
"Spring protected",
"Rainwater",
"Dug well unprotected",
"Spring unprotected",
"Tanker truck / cart",
"Surface water",
"Other",
"node_color"
])
.range([
"#2D20E1",
"#ABA7ED",
"#8726BE",
"#12B3F3",
"#6178e8",
"#F6F614",
"#BDBD07",
"#F4710E",
"#EEB56E",
"#6F3609"
])'
但是,此代码中设置的自定义颜色并没有像我预期的那样出现在输出桑基图中。
您可能 运行 遇到了 D3/JavaScript 仅比较 LinkGroup 值的第一个单词的问题,即忽略第一个 space 之后的所有内容。您可以通过在链接数据框中和 my_color
.
这是一个完整的可重现示例...
library(networkD3)
nodes <- read.table(header = TRUE, text = '
name node_group
"Node 0" "node_color"
"Node 1" "node_color"
"Node 2" "node_color"
"Node 3" "node_color"
"Node 4" "node_color"
"Node 5" "node_color"
"Node 6" "node_color"
"Node 7" "node_color"
"Node 8" "node_color"
"Node 9" "node_color"
"Node 10" "node_color"
')
links <- read.table(header = TRUE, text = '
source target value link_group
0 10 1 "Piped Water"
1 10 1 "Dug well protected"
2 10 1 "Tube well, borehole"
3 10 1 "Spring protected"
4 10 1 "Rainwater"
5 10 1 "Dug well unprotected"
6 10 1 "Spring unprotected"
7 10 1 "Tanker truck / cart"
8 10 1 "Surface water"
9 10 1 "Other"
')
links$link_group <- gsub(" ", "_", links$link_group)
my_color <- paste0(
'd3.scaleOrdinal()
.domain(["',
paste0(links$link_group, collapse = '", "'),
'"])
.range([
"#F00", // red
"#0F0", // green
"#00F", // blue
"#FF0", // yellow
"#71A", // purple
"#EEE", // off-white
"#666", // grey
"#000", // black
"#520", // brown
"#FA0", // orange
"#AAA"
])')
sankeyNetwork(
Links = links,
Nodes = nodes,
Source = "source",
Target = "target",
Value = "value",
NodeID = "name",
NodeGroup = "node_group",
LinkGroup = "link_group",
colourScale = my_color
)