简体   繁体   中英

How to move Sankey first node text to left

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?

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);
  }
')

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