networkD3
包创建的桑基图,我想修改节点和链接的颜色和透明度。我的数据
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年的答案:这里,但我认为这不再起作用,因为我调用这些提出的解决方案之一时会出现错误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"))