我在networkD3
包中创建了一个sankey图。我想修改节点和链接的颜色和透明度。
我的数据networkD3_data
附加在最后。
问题1:如何使用用户定义的调色板修改节点颜色?
我不确定如何使用用户定义的调色板修改颜色。我必须使用特定于每个节点源的相同调色板来与我拥有的其他图表保持一致。
目前,我可以通过定义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
参数,它带有"字符串,用于指定节点的分类颜色标度"。我这意味着我可以在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")
但这不起作用,没有产生情节。我搜索了stackoverflow并找到了2014年的答案:here,但我不认为这有效,因为我收到错误Error: unexpected symbol
调用这些提议的解决方案之一(或者我可能不会知道如何正确实现这一点。
d3.scale.ordinal().range(["#7d3945","#e0677b", "#244457"])
d3.scaleOrdinal().range(["#7d3945","#e0677b", "#244457"])
问题2:是否可以定义源和目标的显示顺序?
那么源从Source0到Source10列出,目标从Target11到Target47?这套在哪里?我知道这对于允许算法最佳地定位节点是违反直觉的。
问题3:如何使用用户定义的调色板修改链接颜色并更改透明度/不透明度?
我还想使用与源相同的颜色方案对源发出的链接进行颜色编码。我可以通过定义LinkGroup
来做到这一点,见下图。我再次接近我想要的,但我只需要指定自己使用的颜色,我不知道在哪里修改它。我还想调整不透明度,使源颜色比链接颜色更坚固。
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"))
答案 0 :(得分:3)
颜色托盘由colourScale
参数确定。它应该是一个包含有效D3代码的字符串来定义调色板。在使用D3v4 +的当前版本networkD3
中,语法d3.scaleOrdinal().range(["#7d3945", "#e0677b", "#244457"])
有效,但可以缩短为d3.scaleOrdinal(["#7d3945", "#e0677b", "#244457"])
。还有其他可能性,see here。您选择/定义的调色板中的颜色数必须等于或大于数据中定义的唯一组的总数,否则它将循环回到调色板的开头,以便为其他组指定颜色。数据中定义的第一组将在调色板中分配第一种颜色,依此类推。
NodeGroup
和LinkGroup
参数分别定义Nodes
和Links
data.frames中列的名称,用于定义每个节点的组值/链接。节点和链接将根据其组值以及基于所使用的调色板指定的颜色进行着色。
将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)
透明度/不透明度可以通过在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)