R forceNetwork - 启用缩放后如何将图例保留在左上角?

R forceNetwork - how do I keep the legend in the top left corner when zooming is enabled?

希望有解决办法。我想在 networkD3 包的 forceNetwork 函数中启用缩放选项,并在放大和缩小时强制图例保留在左上角。在下面的示例中,如果放大和缩小,图例不会保留在左上角。我在使用我自己的数据集的 R Shiny 应用程序中有一个类似的网络。有人知道怎么做吗?

library(networkD3)

data(MisLinks)
data(MisNodes)

# Create graph
forceNetwork(Links = MisLinks, Nodes = MisNodes, Source = "source",
             Target = "target", Value = "value", NodeID = "name",
             Group = "group", opacity = 1, zoom = T, bounded = F,legend = T)

这是一种将所有图例元素移动到与该缩放图层不同的组的方法,这样它们就不会与其他元素一起缩放...

library(networkD3)
library(htmlwidgets)

data(MisLinks)
data(MisNodes)

# Create graph
fn <- forceNetwork(Links = MisLinks, Nodes = MisNodes, Source = "source",
             Target = "target", Value = "value", NodeID = "name",
             Group = "group", opacity = 1, zoom = T, bounded = F,legend = T)

htmlwidgets::onRender(fn, jsCode = '
  function (el, x) {
    d3.select("svg").append("g").attr("id", "legend-layer");
    var legend_layer = d3.select("#legend-layer");
    d3.selectAll(".legend")
      .each(function() { legend_layer.append(() => this); });
  }
')