悬停在 R 中的 networkD3 图中时如何消除淡入淡出

How to remove fade when hovering in networkD3 graph in R

我一直在查看使用 in

的示例

我想知道是否可以删除悬停效果,当您将鼠标悬停在图中的某个节点上时,其他所有内容都会消失?请参阅 https://christophergandrud.github.io/networkD3/ 中的 "Interacting with igraph"。

我查看了软件包文档,但似乎没有任何选项可以删除悬停效果。谢谢。

这是一个解决方案。它需要您编辑关联的 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))

我认为最简单的方法是禁用鼠标悬停功能...

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); }")