Put line break in node label in sankey networkD3 diagram

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

Update: I think the answer to my question is that you cannot put line breaks. A colleague pointed out to me that node labels are SVG blocks that do not support line breaks.

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

How to put line break in node labels for sankey diagram created using networkD3 R package?

Borrowing an example from Put text values ​​to the right of the sankey diagram. I can add values ​​to the tables:

library(networkD3)
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])

#### Need to hover to get counts
##sankeyNetwork(Links=links, Nodes=nodes, Source='src', Target='target',
##  Value='value', NodeID='name', fontSize=16)

## 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
sankeyNetwork(Links=links, Nodes=nodes, Source='src', Target='target',
  Value='value', NodeID='name', fontSize=16, width=600, height=300)

I was hoping I could naively customize paste0to include a line break character, for example:

 name := paste0(name, "\n ", txt$total)

or

name := paste0(name, "<br/> ", txt$total)

But I couldn’t get anything to work, and my JavaScript is too rusty to try to fix it after creating it.

+6

All Articles