R package networkD3为R用户提供了在D3中制作网络图的界面,但输入数据和将数据映射到网络美学的能力与可用图表(例如,simpleNetwork,forceNetwork)不同。
最近,我一直在尝试将自定义颜色应用于radialNetwork()中的每个节点,类似于this thread,但我无法弄清楚自定义着色是如何映射到节点的
生成自定义着色的代码:
library(networkD3)
library(tidyverse)
library(data.tree)
## Data
input <- list(number=50)
Data_tree <- data.frame(Start="Class",
Asset = sample(c("FI","Equity","Currency","Commodities"),input$number,replace = TRUE),
Sub_Asset = sample(c("Asia","Europe","USA","Africa","ME"),input$number,replace = TRUE),
Ticker = replicate(input$number,paste0(sample(LETTERS,3),collapse=""))) %>%
unite(col="pathString",Start,Asset,Sub_Asset,Ticker,sep="-",remove=FALSE) %>%
select(-Start) %>% as.Node(pathDelimiter = "-")
colorVector <- c("black", "red", "blue", "green", "orange",
rep("red", 5), rep("blue", 5), rep("green", 4), rep("orange", 4),
rep("red", 11), rep("blue", 14), rep("green", 14), rep("orange", 11))
jsarray <- paste0('["', paste(colorVector, collapse = '", "'), '"]')
nodeStrokeJS <- JS(paste0('function(d, i) { return ', jsarray, '[i]; }'))
radialNetwork(ToListExplicit(Data_tree, unname = TRUE ),
linkColour = "#ccc",
nodeColour = "#fff",
nodeStroke = nodeStrokeJS,
textColour = "#cccccc")
我认为这个例子有点不完整,因为它没有解释颜色如何映射到节点(是通过ToListExplicit列表列表输出还是data_tree?我不能这样做找到匹配的长度),特别是因为向colorVector添加其他元素并不会破坏脚本。那么如何在radialNetwork()的输入数据中实现自定义颜色?