christophergandrud / networkD3

D3 JavaScript Network Graphs from R
http://christophergandrud.github.io/networkD3
652 stars 269 forks source link

Problems: NOT AVAILABLE for more choice in colourScale parameter of forceNetwork #248

Closed huangwb8 closed 5 years ago

huangwb8 commented 5 years ago

forceNetwork use JS("d3.scaleOrdinal(d3.schemeCategory20);") as default in colourScale parameter. However,sometimes I want some enhanced funtion.

For example: 1.Use other categorical color scale in d3-scale-chromatic such as d3.schemeAccent.I set colourScale as JS("d3.scaleOrdinal(d3.schemeAccent);") but it does not work.I test all the choice and found that d3.schemeCategory20 is the only available one.Is there any notice when I try use other Accent color scheme in networkD3 package in R? 2.Use self-defined color.Sometimes the number of category is small(like 2) and I want use just "red" and "blue" for categorization.But I don't know how to do it because it seem I must use official strategy like JS("d3.scaleOrdinal(d3.schemeCategory20);") and it's hard for a JavaScript newbie to create a JS object.Are there any good methods to do it in R?

The first scene is more important for me.The second is only my whim.Thanks!

cjyetman commented 5 years ago

The only categorical color schemes that are built-in to base D3 v4.5.0 (what's in the current release version of networkD3) are: d3.schemeCategory10 d3.schemeCategory20 d3.schemeCategory20b d3.schemeCategory20c

those work as expected in my testing

d3.schemeAccent and the like are part of the module d3-scale-chromatic, which is not included in networkD3, and is not included in the base release of D3 v4.5.0

There's a somewhat extensive discussion here about how to use a custom color palette. Here's a quick example (be careful with the quotes, that trips a lot of people up)...

library(networkD3)

forceNetwork(Links = MisLinks, Nodes = MisNodes, Source = "source", 
             Target = "target", Value = "value", NodeID = "name", 
             Group = "group", opacity = 1,
             colourScale = JS('d3.scaleOrdinal(["#F00", "#0F0", "#00F"])'))

rplot01

huangwb8 commented 5 years ago

Nice answer!Thank you very much!