用于radialNetwork()的R networkD3颜色节点笔划 [英] R networkD3 color node stroke for radialNetwork()

查看:157
本文介绍了用于radialNetwork()的R networkD3颜色节点笔划的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

程序包包含一些非常好的功能用于创建明显的网络.不同的函数具有不同的论点,因此此问题专门针对radialNetwork()函数. 节点笔触的颜色是否可能根据给定的矢量而改变?

The networkd3 package contains some very nice functions for creating the obvious networks. Different functions have different arguments, so this questions is specifically in regards to the radialNetwork() function. Is it possible to have the color of the node stroke change according to a given vector?

下面的代码创建了一个网络图,但是将节点笔划的参数更改为包含不同颜色的向量无效.

The below code creates a the network graph but changing the argument for the node stroke to a vector containing different colors does not work.

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 = "-")



radialNetwork(ToListExplicit(Data_tree, unname = TRUE ), 
           linkColour = "#ccc",
           nodeColour = "#fff",
           nodeStroke = "orange",
           textColour = "#cccccc")

推荐答案

不直接... 不支持该功能.但是,您可以将自己的JavaScript注入该属性.

Not directly... networkd3 does not support that capability. However, you can inject your own JavaScript into that attribute.

例如,如果您具有正确顺序的颜色名称矢量,则可以将其转换为JavaScript数组(格式为["red", "blue", "green"]),并使用htmlwidget包中的JS函数进行构建通过nodeStroke参数传递的函数.

For example, if you have a vector of color names in the proper order, you could convert that to a JavaScript array (in the form: ["red", "blue", "green"]) and use the JS function from the htmlwidget package to build a function to pass through the nodeStroke argument.

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")

这篇关于用于radialNetwork()的R networkD3颜色节点笔划的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆