简体   繁体   中英

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

I have long(ish) node labels for my Sankey diagram that I would like to rotate so that they can be read top-to-bottom instead of left-to-right. Ideally, I would be able to place these rotated node labels directly over the nodes (rather than the edges). Is there anything like the 'srt' option in base R plots? 带有需要旋转的节点标签的桑基图

You could add JavaScript to the HTMLWidgets to change certain text 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

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM