networkD3 包:悬停时显示所有连接节点的节点名称

networkD3 package: show node names of all connected nodes when hovering over

使用 networkD3 包的 forceNetwork 函数,可以创建交互式网络图,将鼠标悬停在节点上时可以显示节点名称。

我正在尝试创建一个图表,它不仅显示鼠标悬停的节点,还显示所有相邻节点,即直接连接到所选节点的所有节点。但是,它不应显示任何未直接连接到该节点的节点。

虽然我找到了参数opacityNoHover,但它会影响鼠标未覆盖的所有节点,而不仅仅是直接连接的节点。

library(networkD3)
# example data
data(MisLinks)
data(MisNodes)

# creating the plot
 forceNetwork(Links = MisLinks, Nodes = MisNodes,
         Source = "source", Target = "target",
         Value = "value", NodeID = "name",
         Group = "group", opacity = 1, fontSize = 15,
         opacityNoHover = 0)

您可以 re-write mouseovermouseout 函数并用 htmlwidgets::onRender...

覆盖它们
library(networkD3)
library(htmlwidgets)

data(MisLinks)
data(MisNodes)

fn <- forceNetwork(Links = MisLinks, Nodes = MisNodes, Source = "source", 
                   Target = "target", Value = "value", NodeID = "name",
                   Group = "group", opacity = 1, fontSize = 15, 
                   opacityNoHover = 0)
customJS <- '
function(el,x) { 
    var link = d3.selectAll(".link")
    var node = d3.selectAll(".node")

    var options = { opacity: 1,
                    clickTextSize: 10,
                    opacityNoHover: 0.1,
                    radiusCalculation: "Math.sqrt(d.nodesize)+6"
                  }

    var unfocusDivisor = 4;

    var links = HTMLWidgets.dataframeToD3(x.links);
    var linkedByIndex = {};

    links.forEach(function(d) {
      linkedByIndex[d.source + "," + d.target] = 1;
      linkedByIndex[d.target + "," + d.source] = 1;
    });

    function neighboring(a, b) {
      return linkedByIndex[a.index + "," + b.index];
    }

    function nodeSize(d) {
            if(options.nodesize){
                    return eval(options.radiusCalculation);
            }else{
                    return 6}
    }

    function mouseover(d) {
      var unfocusDivisor = 4;

      link.transition().duration(200)
        .style("opacity", function(l) { return d != l.source && d != l.target ? +options.opacity / unfocusDivisor : +options.opacity });

      node.transition().duration(200)
        .style("opacity", function(o) { return d.index == o.index || neighboring(d, o) ? +options.opacity : +options.opacity / unfocusDivisor; });

      d3.select(this).select("circle").transition()
        .duration(750)
        .attr("r", function(d){return nodeSize(d)+5;});

      node.select("text").transition()
        .duration(750)
        .attr("x", 13)
        .style("stroke-width", ".5px")
        .style("font", 24 + "px ")
        .style("opacity", function(o) { return d.index == o.index || neighboring(d, o) ? 1 : 0; });
    }

    function mouseout() {
      node.style("opacity", +options.opacity);
      link.style("opacity", +options.opacity);

      d3.select(this).select("circle").transition()
        .duration(750)
        .attr("r", function(d){return nodeSize(d);});
      node.select("text").transition()
        .duration(1250)
        .attr("x", 0)
        .style("font", options.fontSize + "px ")
        .style("opacity", 0);
    }

    d3.selectAll(".node").on("mouseover", mouseover).on("mouseout", mouseout);
}
'
onRender(fn, customJS)