2

I have been looking at the examples for using in

I wanted to know if it would be possible to remove the hover effect where when you hover over a certain node in the graph everything else fades? See "Interacting with igraph" in https://christophergandrud.github.io/networkD3/.

I looked at the package documentation but there didn't seem to be any option to remove the hover effect. Thanks.

CJ Yetman
  • 8,373
  • 2
  • 24
  • 56
Tillie
  • 33
  • 4
  • That's not a built-in feature, but a reasonable suggestion... you can submit a feature request at https://github.com/christophergandrud/networkD3/issues – CJ Yetman Jan 02 '19 at 08:15

2 Answers2

1

I think the easiest way to do this is to disable the mouseover function...

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); }")
CJ Yetman
  • 8,373
  • 2
  • 24
  • 56
  • Thank you. I tried this although it removes the transparency on hover, it also removes the labels on hover. I would like to keep the labels. – Tillie Jan 02 '19 at 17:33
  • You can add the `opacityNoHover = 1` parameter to make the labels be non-transparent all the time. If you want some/any action to take place on mouseover other than the default (rather than no action), than you will have to overwrite the mouseover function with a new one. – CJ Yetman Jan 02 '19 at 19:27
0

Here is a solution. It requires you edit the associated JS file. I don't believe there is an easier way around this. You could easily create a function out of the below code though.

# 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))
Khaynes
  • 1,976
  • 2
  • 15
  • 27