Put line break in node labels in networkD3 sankey diagram

前端 未结 1 1059
时光说笑
时光说笑 2021-02-14 04:59

++++++++++++++++

Update: I think the answer to my question is that you can\'t put line breaks in. A colleague pointed out to me the node labels are SVG

1条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-14 05:59

    You could replace the SVG text elements with blocks of text/html. This example would need a lot of additional formatting/positioning to be useful, but it demonstrates that it is possible...

    library(networkD3)
    library(htmlwidgets)
    library(data.table)
    
    set.seed(1999)
    links <- data.table(
      src = rep(0:4, times=c(1,1,2,3,5)),
      target = sample(1:11, 12, TRUE),
      value = sample(100, 12)
    )[src < target, ]  # no loops
    nodes <- data.table(name=LETTERS[1:12])
    
    ## Add text to label
    txt <- links[, .(total = sum(value)), by=c('target')]
    nodes[txt$target+1L, name := paste0(name, '
    (', txt$total, ')')] ## Displays the counts as part of the labels sn <- sankeyNetwork(Links=links, Nodes=nodes, Source='src', Target='target', Value='value', NodeID='name', fontSize=16, width=600, height=300) onRender(sn, ' function(el,x) { d3.selectAll(".node text").remove() d3.selectAll(".node") .append("foreignObject") .attr("width", 100) .attr("height", 50) .html(function(d) { return d.name; }) } ' )

    0 讨论(0)
提交回复
热议问题