【问题标题】:How to move Sankey first node text to leftHow to move Sankey first node text to left
【发布时间】:2022-12-19 05:35:54
【问题描述】:

I am creating a sankey plot (migration form):

library(tidyverse)
library(networkD3)
library(htmlwidgets)

links <- data.frame(source = c("a","b","a","a","c","c","d","e","e"), 
                            target = c("e","a","a","c","a","c","a","a","e"), 
                            value = c(453, 244,3585,1055,1027,643,1021,692,268))

# only way I could figure out to get same source and target node names
links$target <- paste(links$target, " ", sep="")

nodes <- data.frame(
        name=c(as.character(links$source),
               as.character(links$target)) %>% unique)

links$IDsource <- match(links$source, nodes$name)-1
links$IDtarget <- match(links$target, nodes$name)-1

sankey <- sankeyNetwork(Links = links, Nodes = nodes,
                        Source = "IDsource", Target = "IDtarget",
                        Value = "value", NodeID = "name",
                        sinksRight=FALSE)

does anyone know how to make it so the first node text is moved to the left of the border of the plot? kind of like sinksRight=TRUE but for the left side?

Thanks!

【问题讨论】:

    标签: r d3.js sankey-diagram htmlwidgets networkd3


    【解决方案1】:

    You could use htmlwidgets::onRender() to add some JavaScript to move the node text to the left of the nodes.

    htmlwidgets::onRender(sankey, jsCode = '
      function(el, x) {
        d3.selectAll(".node text")
          .attr("x", -6);
      }
    ')
    

    【讨论】:

      猜你喜欢
      • 2011-04-06
      • 2022-12-16
      • 2022-12-28
      • 2022-12-02
      • 2022-07-28
      • 2022-12-01
      • 2022-12-27
      • 1970-01-01
      相关资源
      最近更新 更多