如何在 R 中的 forcenetwork 中设置链接距离?
How can I set linkdistance in forcenetwork in R?
我正在尝试在 networkd3 包中使用 forceNetwork
。我想定义一个 linkDistance
以便为每个节点设置特定的距离。
但是我有一个问题 ()。那里的答案没有解决我的问题。
这是我的数据示例和代码。
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)
我试试这个。但是没有解决。
linkDistance = networkD3::JS("function(d) { return 5*d.linkdist; }")
或
linkDistance=JS('function(d) {', 'return d.linkdist;', '}')
networkD3
仅将链接数据框的必要列传递给它创建的 htmlwidget
,因此 d.linkdist
不存在于 JavaScript 的数据中正在使用。您可以在 运行 之前的 htmlwidget
中的数据中添加您想要的列,然后您的自定义 JavaScript 将起作用...(我不得不修改您的示例,以便它是可重现的)...
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
我正在尝试在 networkd3 包中使用 forceNetwork
。我想定义一个 linkDistance
以便为每个节点设置特定的距离。
但是我有一个问题 (
这是我的数据示例和代码。
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)
我试试这个。但是没有解决。
linkDistance = networkD3::JS("function(d) { return 5*d.linkdist; }")
或
linkDistance=JS('function(d) {', 'return d.linkdist;', '}')
networkD3
仅将链接数据框的必要列传递给它创建的 htmlwidget
,因此 d.linkdist
不存在于 JavaScript 的数据中正在使用。您可以在 运行 之前的 htmlwidget
中的数据中添加您想要的列,然后您的自定义 JavaScript 将起作用...(我不得不修改您的示例,以便它是可重现的)...
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