使用用户定义的颜色修改 networkD3 桑基图
Modify networkD3 sankey plot with user-defined colors
我在 networkD3
包中创建了桑基图。我想修改两个节点和 links.
的颜色和透明度
我的数据networkD3_data
在最后附加。
问题一:如何使用自定义调色板修改节点颜色?
我不确定如何使用用户定义的调色板修改颜色。我有必要使用特定于每个节点源的相同调色板,以与我拥有的其他图保持一致。
目前,通过定义 NodeGroup
,我可以让每个源节点具有不同的颜色,而所有目标节点具有相同的颜色。下面的图很接近我想要的,但是现在我想自己指定每个源节点的颜色。
library(networkD3)
sankeyNetwork(Links = networkD3_data$links, Nodes = networkD3_data$nodes, Source = "source", Target = "target", Value = "value", NodeID = "name", NodeGroup="group", fontSize=14)
?sankeyNetwork
表示有一个 colourScale
参数需要 "character string specifying the categorical colour scale for the nodes"。我认为这意味着我可以将具有我想要的颜色的列添加到 networkD3_data$nodes
数据框并这样调用 colourScale
:
sankeyNetwork(Links = networkD3_data$links, Nodes = networkD3_data$nodes, Source = "source", Target = "target", Value = "value", NodeID = "name", NodeGroup="group", fontSize=14, colourScale="colors")
但是这样不行,没有剧情产生。我搜索了 Whosebug 并找到了 2014 年的答案:here,但我认为这不再有效,因为我在调用这些建议的解决方案中的任何一个时遇到错误 Error: unexpected symbol
(或者我可能只是不知道如何正确实施)。
d3.scale.ordinal().range(["#7d3945","#e0677b", "#244457"])
d3.scaleOrdinal().range(["#7d3945","#e0677b", "#244457"])
问题2:是否可以定义源和目标的显示顺序?
那么源是从 Source0 到 Source10 列出的,而目标是从 Target11 到 Target47 列出的吗?这套在哪里?我知道这对于允许算法优化节点定位是违反直觉的。
问题3:如何用自定义调色板修改link颜色并改变transparency/opacity?
我还想使用与源相同的配色方案对从源发出的 link 进行颜色编码。我可以通过定义 LinkGroup
来做到这一点,见下图。我再次接近我想要的,但我只需要指定自己使用的颜色,我不确定在哪里修改它。我还想调整不透明度,使源颜色比 link 颜色更纯。
sankeyNetwork(Links = networkD3_data$links, Nodes = networkD3_data$nodes, Source = "source", Target = "target", Value = "value", NodeID = "name", NodeGroup="group", LinkGroup="group", fontSize=14)
这是我的数据 -- dput(networkD3_data)
structure(list(nodes = structure(list(name = c("Source0", "Source1",
"Source2", "Source3", "Source4", "Source5", "Source6", "Source7",
"Source8", "Source9", "Source10", "Target11", "Target12", "Target13",
"Target14", "Target15", "Target16", "Target17", "Target18", "Target19",
"Target20", "Target21", "Target22", "Target23", "Target24", "Target25",
"Target26", "Target27", "Target28", "Target29", "Target30", "Target31",
"Target32", "Target33", "Target34", "Target35", "Target36", "Target37",
"Target38", "Target39", "Target40", "Target41", "Target42", "Target43",
"Target44", "Target45", "Target46", "Target47"), group = c("Source0",
"Source1", "Source2", "Source3", "Source4", "Source5", "Source6",
"Source7", "Source8", "Source9", "Source10", "Target", "Target",
"Target", "Target", "Target", "Target", "Target", "Target", "Target",
"Target", "Target", "Target", "Target", "Target", "Target", "Target",
"Target", "Target", "Target", "Target", "Target", "Target", "Target",
"Target", "Target", "Target", "Target", "Target", "Target", "Target",
"Target", "Target", "Target", "Target", "Target", "Target", "Target"
), colors = c("#9E0142", "#D53E4F", "#F46D43", "#FDAE61", "#FEE08B",
"#FFFFBF", "#E6F598", "#ABDDA4", "#66C2A5", "#3288BD", "#5E4FA2",
"#969696", "#969696", "#969696", "#969696", "#969696", "#969696",
"#969696", "#969696", "#969696", "#969696", "#969696", "#969696",
"#969696", "#969696", "#969696", "#969696", "#969696", "#969696",
"#969696", "#969696", "#969696", "#969696", "#969696", "#969696",
"#969696", "#969696", "#969696", "#969696", "#969696", "#969696",
"#969696", "#969696", "#969696", "#969696", "#969696", "#969696",
"#969696")), .Names = c("name", "group", "colors"), row.names = c(NA,
-48L), class = "data.frame"), links = structure(list(source = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6,
6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
8, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10, 10
), target = c(11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22,
23, 13, 18, 15, 11, 12, 24, 21, 25, 26, 27, 19, 28, 16, 22, 29,
30, 31, 32, 18, 16, 15, 13, 27, 29, 19, 33, 34, 31, 35, 21, 24,
11, 30, 36, 28, 37, 38, 39, 40, 26, 41, 11, 12, 15, 18, 19, 14,
13, 16, 27, 34, 20, 22, 25, 12, 27, 16, 18, 13, 11, 12, 11, 14,
27, 21, 16, 18, 22, 13, 15, 19, 16, 11, 12, 39, 12, 14, 18, 11,
42, 43, 44, 13, 11, 18, 15, 12, 19, 45, 31, 16, 20, 46, 40, 47,
11, 12, 18, 16, 14, 19, 15, 11, 12, 16, 13, 18, 14, 34, 31, 15
), value = c(5.8, 3.2, 5, 2.4, 2.5, 2.7, 3.5, 2.5, 3.5, 1.4,
2.9, 2.4, 1.3, 12.1, 7.4, 5, 11.2, 5.6, 6.4, 8.8, 2.6, 3.5, 7,
10, 4.5, 6, 6.5, 5.8, 5.4, 6.2, 8.9, 5.5, 4.8, 3.4, 6.5, 5, 4,
6.4, 7.3, 4.4, 4.2, 1.7, 5.1, 3.6, 6.4, 3.4, 2.5, 2.6, 2.3, 2.3,
3.2, 1.6, 1.7, 3.7, 8, 4.4, 3.1, 4.1, 5.9, 2.8, 5, 3.2, 3.7,
3.4, 1.8, 3.2, 1.2, 4.1, 5.2, 4.5, 4.8, 7.1, 7.3, 4.6, 8.4, 3.4,
5.2, 6.1, 4.3, 4.5, 4.5, 6.5, 2.8, 6.3, 5.3, 8.2, 3.8, 4.3, 4.2,
3.4, 5.4, 7.9, 1.2, 1.4, 1.4, 6.6, 6.8, 4.2, 2.9, 3.1, 5.3, 2.6,
3.2, 2.9, 1.7, 1.9, 1.4, 8, 8, 4, 5, 4.3, 2.9, 6.9, 3, 8.7, 4.5,
4.2, 6.6, 4.4, 2.7, 4.4, 4.3, 2.8), group = c("Source0", "Source0",
"Source0", "Source0", "Source0", "Source0", "Source0", "Source0",
"Source0", "Source0", "Source0", "Source0", "Source0", "Source1",
"Source1", "Source1", "Source1", "Source1", "Source1", "Source1",
"Source1", "Source1", "Source1", "Source1", "Source1", "Source1",
"Source1", "Source1", "Source1", "Source1", "Source1", "Source2",
"Source2", "Source2", "Source2", "Source2", "Source2", "Source2",
"Source2", "Source2", "Source2", "Source2", "Source2", "Source2",
"Source2", "Source2", "Source2", "Source2", "Source2", "Source2",
"Source2", "Source2", "Source2", "Source2", "Source3", "Source3",
"Source3", "Source3", "Source3", "Source3", "Source3", "Source3",
"Source3", "Source3", "Source3", "Source3", "Source3", "Source4",
"Source4", "Source4", "Source4", "Source4", "Source4", "Source5",
"Source5", "Source5", "Source5", "Source5", "Source5", "Source5",
"Source5", "Source5", "Source5", "Source5", "Source6", "Source6",
"Source6", "Source6", "Source7", "Source7", "Source7", "Source7",
"Source7", "Source7", "Source7", "Source8", "Source8", "Source8",
"Source8", "Source8", "Source8", "Source8", "Source8", "Source8",
"Source8", "Source8", "Source8", "Source9", "Source9", "Source9",
"Source9", "Source9", "Source9", "Source9", "Source9", "Source10",
"Source10", "Source10", "Source10", "Source10", "Source10", "Source10",
"Source10", "Source10")), .Names = c("source", "target", "value",
"group"), row.names = c(NA, -124L), class = "data.frame")), .Names = c("nodes",
"links"))
调色板由 colourScale
参数决定。它应该是一个字符串,其中包含用于定义调色板的有效 D3 代码。在使用 D3v4+ 的 networkD3
的当前版本中,语法 d3.scaleOrdinal().range(["#7d3945", "#e0677b", "#244457"])
是有效的,尽管它可以缩短为 d3.scaleOrdinal(["#7d3945", "#e0677b", "#244457"])
。还有其他可能性,see here。您 choose/define 调色板中的颜色数量必须等于或大于数据中定义的唯一组的总数,否则它将循环回到调色板的开头以将颜色分配给其他组。在您的数据中定义的第一个组将分配给您的调色板中的第一个颜色,依此类推。
NodeGroup
和 LinkGroup
参数分别定义 Nodes
和 Links
data.frames 中定义组值的列的名称每个 node/link。节点和链接都将根据它们的组值和基于所使用的调色板分配的颜色进行着色。
将 iteration
参数设置为 0 将有效地阻止放置算法 运行,因此您的节点将按照它们在原始数据中的顺序进行排序。正如您所指出的,这基本上违背了 sankeyNetwork 的主要目的。
使用您发布的数据...
library(networkD3)
colors <- paste(networkD3_data$nodes$colors, collapse = '", "')
colorJS <- paste('d3.scaleOrdinal(["', colors, '"])')
sankeyNetwork(Links = networkD3_data$links, Nodes = networkD3_data$nodes,
Source = 'source', Target = 'target', Value = 'value',
NodeID = 'name', NodeGroup = "group", LinkGroup = "group",
colourScale = colorJS,
iterations = 0)
Transparency/opacity 可以通过在 D3 调色板中设置 RGBA 颜色来实现,但是您需要使用十进制 RGB 表示法(据我所知)。您需要将 #ff0043
之类的内容转换为 d3.rgb(255,0,67,0.5)
之类的内容,其中最后一个数字是 0 到 1 之间的数字,用于定义不透明度级别。例如...
colors <- paste(sapply(networkD3_data$nodes$colors, function(x) { paste0("d3.rgb(", paste(c(col2rgb(x), 0.5), collapse = "," ), ")") }), collapse = ", ")
colorJS <- paste0('d3.scaleOrdinal([', colors, '])')
sankeyNetwork(Links = networkD3_data$links, Nodes = networkD3_data$nodes,
Source = 'source', Target = 'target', Value = 'value',
NodeID = 'name', NodeGroup = "group", LinkGroup = "group",
colourScale = colorJS,
iterations = 0)
我在 networkD3
包中创建了桑基图。我想修改两个节点和 links.
我的数据networkD3_data
在最后附加。
问题一:如何使用自定义调色板修改节点颜色?
我不确定如何使用用户定义的调色板修改颜色。我有必要使用特定于每个节点源的相同调色板,以与我拥有的其他图保持一致。
目前,通过定义 NodeGroup
,我可以让每个源节点具有不同的颜色,而所有目标节点具有相同的颜色。下面的图很接近我想要的,但是现在我想自己指定每个源节点的颜色。
library(networkD3)
sankeyNetwork(Links = networkD3_data$links, Nodes = networkD3_data$nodes, Source = "source", Target = "target", Value = "value", NodeID = "name", NodeGroup="group", fontSize=14)
?sankeyNetwork
表示有一个 colourScale
参数需要 "character string specifying the categorical colour scale for the nodes"。我认为这意味着我可以将具有我想要的颜色的列添加到 networkD3_data$nodes
数据框并这样调用 colourScale
:
sankeyNetwork(Links = networkD3_data$links, Nodes = networkD3_data$nodes, Source = "source", Target = "target", Value = "value", NodeID = "name", NodeGroup="group", fontSize=14, colourScale="colors")
但是这样不行,没有剧情产生。我搜索了 Whosebug 并找到了 2014 年的答案:here,但我认为这不再有效,因为我在调用这些建议的解决方案中的任何一个时遇到错误 Error: unexpected symbol
(或者我可能只是不知道如何正确实施)。
d3.scale.ordinal().range(["#7d3945","#e0677b", "#244457"])
d3.scaleOrdinal().range(["#7d3945","#e0677b", "#244457"])
问题2:是否可以定义源和目标的显示顺序?
那么源是从 Source0 到 Source10 列出的,而目标是从 Target11 到 Target47 列出的吗?这套在哪里?我知道这对于允许算法优化节点定位是违反直觉的。
问题3:如何用自定义调色板修改link颜色并改变transparency/opacity?
我还想使用与源相同的配色方案对从源发出的 link 进行颜色编码。我可以通过定义 LinkGroup
来做到这一点,见下图。我再次接近我想要的,但我只需要指定自己使用的颜色,我不确定在哪里修改它。我还想调整不透明度,使源颜色比 link 颜色更纯。
sankeyNetwork(Links = networkD3_data$links, Nodes = networkD3_data$nodes, Source = "source", Target = "target", Value = "value", NodeID = "name", NodeGroup="group", LinkGroup="group", fontSize=14)
这是我的数据 -- dput(networkD3_data)
structure(list(nodes = structure(list(name = c("Source0", "Source1",
"Source2", "Source3", "Source4", "Source5", "Source6", "Source7",
"Source8", "Source9", "Source10", "Target11", "Target12", "Target13",
"Target14", "Target15", "Target16", "Target17", "Target18", "Target19",
"Target20", "Target21", "Target22", "Target23", "Target24", "Target25",
"Target26", "Target27", "Target28", "Target29", "Target30", "Target31",
"Target32", "Target33", "Target34", "Target35", "Target36", "Target37",
"Target38", "Target39", "Target40", "Target41", "Target42", "Target43",
"Target44", "Target45", "Target46", "Target47"), group = c("Source0",
"Source1", "Source2", "Source3", "Source4", "Source5", "Source6",
"Source7", "Source8", "Source9", "Source10", "Target", "Target",
"Target", "Target", "Target", "Target", "Target", "Target", "Target",
"Target", "Target", "Target", "Target", "Target", "Target", "Target",
"Target", "Target", "Target", "Target", "Target", "Target", "Target",
"Target", "Target", "Target", "Target", "Target", "Target", "Target",
"Target", "Target", "Target", "Target", "Target", "Target", "Target"
), colors = c("#9E0142", "#D53E4F", "#F46D43", "#FDAE61", "#FEE08B",
"#FFFFBF", "#E6F598", "#ABDDA4", "#66C2A5", "#3288BD", "#5E4FA2",
"#969696", "#969696", "#969696", "#969696", "#969696", "#969696",
"#969696", "#969696", "#969696", "#969696", "#969696", "#969696",
"#969696", "#969696", "#969696", "#969696", "#969696", "#969696",
"#969696", "#969696", "#969696", "#969696", "#969696", "#969696",
"#969696", "#969696", "#969696", "#969696", "#969696", "#969696",
"#969696", "#969696", "#969696", "#969696", "#969696", "#969696",
"#969696")), .Names = c("name", "group", "colors"), row.names = c(NA,
-48L), class = "data.frame"), links = structure(list(source = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6,
6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
8, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10, 10
), target = c(11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22,
23, 13, 18, 15, 11, 12, 24, 21, 25, 26, 27, 19, 28, 16, 22, 29,
30, 31, 32, 18, 16, 15, 13, 27, 29, 19, 33, 34, 31, 35, 21, 24,
11, 30, 36, 28, 37, 38, 39, 40, 26, 41, 11, 12, 15, 18, 19, 14,
13, 16, 27, 34, 20, 22, 25, 12, 27, 16, 18, 13, 11, 12, 11, 14,
27, 21, 16, 18, 22, 13, 15, 19, 16, 11, 12, 39, 12, 14, 18, 11,
42, 43, 44, 13, 11, 18, 15, 12, 19, 45, 31, 16, 20, 46, 40, 47,
11, 12, 18, 16, 14, 19, 15, 11, 12, 16, 13, 18, 14, 34, 31, 15
), value = c(5.8, 3.2, 5, 2.4, 2.5, 2.7, 3.5, 2.5, 3.5, 1.4,
2.9, 2.4, 1.3, 12.1, 7.4, 5, 11.2, 5.6, 6.4, 8.8, 2.6, 3.5, 7,
10, 4.5, 6, 6.5, 5.8, 5.4, 6.2, 8.9, 5.5, 4.8, 3.4, 6.5, 5, 4,
6.4, 7.3, 4.4, 4.2, 1.7, 5.1, 3.6, 6.4, 3.4, 2.5, 2.6, 2.3, 2.3,
3.2, 1.6, 1.7, 3.7, 8, 4.4, 3.1, 4.1, 5.9, 2.8, 5, 3.2, 3.7,
3.4, 1.8, 3.2, 1.2, 4.1, 5.2, 4.5, 4.8, 7.1, 7.3, 4.6, 8.4, 3.4,
5.2, 6.1, 4.3, 4.5, 4.5, 6.5, 2.8, 6.3, 5.3, 8.2, 3.8, 4.3, 4.2,
3.4, 5.4, 7.9, 1.2, 1.4, 1.4, 6.6, 6.8, 4.2, 2.9, 3.1, 5.3, 2.6,
3.2, 2.9, 1.7, 1.9, 1.4, 8, 8, 4, 5, 4.3, 2.9, 6.9, 3, 8.7, 4.5,
4.2, 6.6, 4.4, 2.7, 4.4, 4.3, 2.8), group = c("Source0", "Source0",
"Source0", "Source0", "Source0", "Source0", "Source0", "Source0",
"Source0", "Source0", "Source0", "Source0", "Source0", "Source1",
"Source1", "Source1", "Source1", "Source1", "Source1", "Source1",
"Source1", "Source1", "Source1", "Source1", "Source1", "Source1",
"Source1", "Source1", "Source1", "Source1", "Source1", "Source2",
"Source2", "Source2", "Source2", "Source2", "Source2", "Source2",
"Source2", "Source2", "Source2", "Source2", "Source2", "Source2",
"Source2", "Source2", "Source2", "Source2", "Source2", "Source2",
"Source2", "Source2", "Source2", "Source2", "Source3", "Source3",
"Source3", "Source3", "Source3", "Source3", "Source3", "Source3",
"Source3", "Source3", "Source3", "Source3", "Source3", "Source4",
"Source4", "Source4", "Source4", "Source4", "Source4", "Source5",
"Source5", "Source5", "Source5", "Source5", "Source5", "Source5",
"Source5", "Source5", "Source5", "Source5", "Source6", "Source6",
"Source6", "Source6", "Source7", "Source7", "Source7", "Source7",
"Source7", "Source7", "Source7", "Source8", "Source8", "Source8",
"Source8", "Source8", "Source8", "Source8", "Source8", "Source8",
"Source8", "Source8", "Source8", "Source9", "Source9", "Source9",
"Source9", "Source9", "Source9", "Source9", "Source9", "Source10",
"Source10", "Source10", "Source10", "Source10", "Source10", "Source10",
"Source10", "Source10")), .Names = c("source", "target", "value",
"group"), row.names = c(NA, -124L), class = "data.frame")), .Names = c("nodes",
"links"))
调色板由 colourScale
参数决定。它应该是一个字符串,其中包含用于定义调色板的有效 D3 代码。在使用 D3v4+ 的 networkD3
的当前版本中,语法 d3.scaleOrdinal().range(["#7d3945", "#e0677b", "#244457"])
是有效的,尽管它可以缩短为 d3.scaleOrdinal(["#7d3945", "#e0677b", "#244457"])
。还有其他可能性,see here。您 choose/define 调色板中的颜色数量必须等于或大于数据中定义的唯一组的总数,否则它将循环回到调色板的开头以将颜色分配给其他组。在您的数据中定义的第一个组将分配给您的调色板中的第一个颜色,依此类推。
NodeGroup
和 LinkGroup
参数分别定义 Nodes
和 Links
data.frames 中定义组值的列的名称每个 node/link。节点和链接都将根据它们的组值和基于所使用的调色板分配的颜色进行着色。
将 iteration
参数设置为 0 将有效地阻止放置算法 运行,因此您的节点将按照它们在原始数据中的顺序进行排序。正如您所指出的,这基本上违背了 sankeyNetwork 的主要目的。
使用您发布的数据...
library(networkD3)
colors <- paste(networkD3_data$nodes$colors, collapse = '", "')
colorJS <- paste('d3.scaleOrdinal(["', colors, '"])')
sankeyNetwork(Links = networkD3_data$links, Nodes = networkD3_data$nodes,
Source = 'source', Target = 'target', Value = 'value',
NodeID = 'name', NodeGroup = "group", LinkGroup = "group",
colourScale = colorJS,
iterations = 0)
Transparency/opacity 可以通过在 D3 调色板中设置 RGBA 颜色来实现,但是您需要使用十进制 RGB 表示法(据我所知)。您需要将 #ff0043
之类的内容转换为 d3.rgb(255,0,67,0.5)
之类的内容,其中最后一个数字是 0 到 1 之间的数字,用于定义不透明度级别。例如...
colors <- paste(sapply(networkD3_data$nodes$colors, function(x) { paste0("d3.rgb(", paste(c(col2rgb(x), 0.5), collapse = "," ), ")") }), collapse = ", ")
colorJS <- paste0('d3.scaleOrdinal([', colors, '])')
sankeyNetwork(Links = networkD3_data$links, Nodes = networkD3_data$nodes,
Source = 'source', Target = 'target', Value = 'value',
NodeID = 'name', NodeGroup = "group", LinkGroup = "group",
colourScale = colorJS,
iterations = 0)