我一直在看在 r 中使用 networkd3 的示例
我想知道是否有可能消除悬停效果,当您将鼠标悬停在图形中的某个节点上时,其他一切都会消失?请参阅 https://christophergandrud.github.io/networkD3/中的"与 igraph 交互"。
我查看了包文档,但似乎没有任何选项可以删除悬停效果。谢谢。
我认为最简单的方法是禁用鼠标悬停功能...
library(networkD3)
library(htmlwidgets)
fn <- forceNetwork(Links = MisLinks, Nodes = MisNodes, Source = "source",
Target = "target", Value = "value", NodeID = "name",
Group = "group", opacity = 1)
onRender(fn, "function(el,x) { d3.selectAll('.node').on('mouseover', null); }")
这是一个解决方案。它要求您编辑关联的 JS 文件。我不相信有更简单的方法可以解决这个问题。不过,您可以轻松地从以下代码中创建一个函数。
# Load package
library(networkD3)
# Create a network graph.
src <- c("A", "A", "A", "A",
"B", "B", "C", "C", "D")
target <- c("B", "C", "D", "J",
"E", "F", "G", "H", "I")
networkData <- data.frame(src, target)
# Create widget.
widget <- simpleNetwork(networkData)
# Define (temporary) path to where the widget will be saved.
file <- tempfile(pattern = "file", tmpdir = dir <- tempdir(), fileext = ".html")
# Save widget.
htmlwidgets::saveWidget(widget, file = file, selfcontained = FALSE)
# Define path to where js file is we want to edit.
js_path <- paste0(dir, "/", gsub(".*\\|\..*", "", file), "_files/forceNetwork-binding-0.4/forceNetwork.js")
# Read in JS file.
js <- readLines(js_path)
# Edit relevant line so that no transparency occurs on hover.
js <- gsub("var unfocusDivisor = 4;", "var unfocusDivisor = 1;", js)
writeLines(js, con = js_path)
# Open graph
browseURL(paste0("file://", file))