我可以在 Sankey Plot (networkD3::sankeyNetwork) 中旋转节点标签吗?

Can I rotate the node labels in a Sankey Plot (networkD3::sankeyNetwork)?

我的 Sankey 图有很长的节点标签,我想旋转它们,以便可以从上到下而不是从左到右阅读它们。理想情况下,我可以将这些旋转的节点标签直接放在节点上(而不是边缘)。基本 R 图中是否有类似 'srt' 选项的内容?

您可以将 JavaScript 添加到 HTMLWidgets 以更改某些文本 attributes/styles...

library(networkD3)
library(htmlwidgets)

links <- data.frame(
  src = c(0, 0, 0, 1, 1, 1, 2, 2, 2),
  target = c(3, 4, 5, 3, 4, 5, 3, 4, 5),
  value = 1
)

nodes <- data.frame(name = paste0("node", 1:6))

sn <- sankeyNetwork(
  Links = links,
  Nodes = nodes,
  Source = 'src',
  Target = 'target',
  Value = 'value',
  NodeID = 'name',
  fontSize = 16,
  width = 600,
  height = 300,
  margin = list("left" = 100)
)

sn <- onRender(
  sn,
  '
  function(el,x) {
    d3.select(el)
      .selectAll(".node text")
      .attr("text-anchor", "middle")
      .style("writing-mode", "vertical-rl")
      .style("text-orientation", "upright");
  }
  '
)

sn