1

I am trying to use forceNetwork in the package. I want to define a linkDistance in order to set a particular distance for each node. But I have a problem (same as this question). The answer there does not solve my problem.

Here is my data example and code.

library(tibble)
library(networkD3)
library(htmlwidgets)

##Data
link_df <-
  tibble::tribble(
   ~source, ~target, ~weight,   ~source_idx, ~target_idx, ~linkdist,
   "great", "great", 1.0000000, 0,           0,          10.000000,
   "good",  "great", 0.7036672, 1,           0,          7.036672,
   "best",  "great", 0.3486529, 2,           0,          3.486529,
   "win",   "great", 0.2147706, 3,           0,          2.147706
  )

node_df <-
  tibble::tribble(
    ~node,    ~degree,   ~sent, ~node_size,
    "great",    0.6817043, "A",   64.789598,
    "good", 0.5037594, "A",   19.320302,
    "best", 0.4761905, "B",   15.425671,
    "win",  0.6190476, "C",   44.057260
  )

##Code
ColourScale <- 'd3.scaleOrdinal()
    .domain(["A", "B", "C", "D"])
   .range(["#ffdd00", "#ff0000", "#0004ff", "#9500ff"]);'

forceNetwork(Links = link_df, Nodes = node_df, 
             Source = 'source_idx', Target = 'target_idx', 
             NodeID = 'node', Group = 'sent',
             Nodesize = 'node_size', 
             bounded = TRUE, opacityNoHover = TRUE, zoom = TRUE,
             colourScale = JS(ColourScale),
             legend = TRUE,
             fontFamily = "Calibri",
             fontSize = 20,
             charge = -400,
             opacity = 0.8)

I try this. But it didn't solve.

linkDistance = networkD3::JS("function(d) { return 5*d.linkdist; }")

or

linkDistance=JS('function(d) {', 'return d.linkdist;', '}')
CJ Yetman
  • 8,373
  • 2
  • 24
  • 56
HYljy
  • 15
  • 6

1 Answers1

1

networkD3 only passes on the necessary columns of your links data frame to the htmlwidget that it creates, so d.linkdist does not exist in the data that JavaScript is using. You can add back in the columns you want to the data in the htmlwidget before running it and then your custom JavaScript will work... (I had to modify your example so that it was reproducible)...

library(tibble)
library(networkD3)
library(htmlwidgets)

##Data
link_df <-
  tibble::tribble(
   ~source, ~target, ~weight,   ~source_idx, ~target_idx, ~linkdist,
   "great", "great", 1.0000000, 0,           0,          10.000000,
   "good",  "great", 0.7036672, 1,           0,          7.036672,
   "best",  "great", 0.3486529, 2,           0,          3.486529,
   "win",   "great", 0.2147706, 3,           0,          2.147706
  )

node_df <-
  tibble::tribble(
    ~node,    ~degree,   ~sent, ~node_size,
    "great",    0.6817043, "A",   64.789598,
    "good", 0.5037594, "A",   19.320302,
    "best", 0.4761905, "B",   15.425671,
    "win",  0.6190476, "C",   44.057260
  )

##Code
ColourScale <- 'd3.scaleOrdinal()
    .domain(["A", "B", "C", "D"])
   .range(["#ffdd00", "#ff0000", "#0004ff", "#9500ff"]);'

fn <- forceNetwork(Links = link_df, Nodes = node_df, 
             Source = 'source_idx', Target = 'target_idx', 
             NodeID = 'node', Group = 'sent',
             Nodesize = 'node_size', 
             bounded = TRUE, opacityNoHover = TRUE, zoom = TRUE,
             colourScale = JS(ColourScale),
             legend = TRUE,
             fontFamily = "Calibri",
             fontSize = 20,
             charge = -400,
             opacity = 0.8,
             linkDistance=JS('function(d) {', 'return d.linkdist;', '}'))

fn$x$links$linkdist <- link_df$linkdist
fn

enter image description here

CJ Yetman
  • 8,373
  • 2
  • 24
  • 56