本文介绍了在forceNetwork for networkD3中更改图例文本的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用networkD3
R包中的forceNetwork
,如何将图例中文本的颜色更改为白色?
Using forceNetwork
from the networkD3
R package, how can I change the color of the text in the legend to white?
我的代码:
library(networkD3)
forceNetwork(Links = subLinkList, Nodes = subNodes,
Source = "root", Target = "children",
Value = "linkValue", NodeID = "nodeName",
Nodesize = "nodeSize", Group = "nodeGroup",
colourScale = JS(colorScale), charge = -500,
opacity = 1, opacityNoHover = 1, fontSize = 25,
fontFamily = "Calibri",
linkDistance = JS('function(){d3.select("body").style("background-color", "#144370");return 200;}'),
linkWidth = 3, linkColour = "white",
legend = TRUE, bounded = TRUE, zoom = TRUE)
我尝试过的事情:
linkDistance = JS('function(){d3.select("body").style("background-color", "#144370").legend.text("fill", "white");return 200;}')
推荐答案
这是一个完全可复制的示例,使用更好的方法来添加自定义JavaScript而不是使用额外的代码来重载linkDistance
参数...
this is a fully replicable example, using a better method to add custom JavaScript than overloading the linkDistance
argument with extra code...
library(networkD3)
library(htmlwidgets)
subNodes <-
read.table(stringsAsFactors = FALSE, header = TRUE, text = "
nodeName nodeGroup nodeSize
Bob NorthAmerica 10
Alice NorthAmerica 10
Tom China 10
John Japan 10
")
subLinkList <-
read.table(stringsAsFactors = FALSE, header = TRUE, text = "
root children linkValue
0 1 1
0 2 1
0 3 1
")
colorScale <- "d3.scaleOrdinal(d3.schemeCategory20);"
fn <- forceNetwork(Links = subLinkList, Nodes = subNodes,
Source = "root", Target = "children",
Value = "linkValue", NodeID = "nodeName",
Nodesize = "nodeSize", Group = "nodeGroup",
colourScale = JS(colorScale),
charge = -500, opacity = 1, opacityNoHover = 1, fontSize = 25,
fontFamily = "Calibri", linkDistance = 200,
linkWidth = 3, linkColour = "white",
legend = TRUE, bounded = TRUE, zoom = TRUE)
htmlwidgets::onRender(
fn,
'function(el, x) {
d3.select("body").style("background-color", "#144370");
d3.selectAll(".legend text").style("fill", "white");
}'
)
这篇关于在forceNetwork for networkD3中更改图例文本的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!