how to insert values in sankey chart?

Hello!
I would like the value to be visualized on the Sankey chart.
how could i do it?
I need the graph in image format but when saving the value of the nodes is not displayed.
This is the code I used:
library(tidyverse)
library(readr)
library(openxlsx)
library(xlsx)
library(dplyr)
library(readxl)
library(networkD3)

datosankey <- read.csv("https://docs.google.com/spreadsheets/d/e/2PACX-1vS45SekhiRvzq8PXATIpG9HjwqNBloVGBuey4Zmf7eM0CPTFKGeT7ZnB_KaSKml_gPF5gzt5nHFbQUs/pub?output=csv")

datosankey$target <- paste(datosankey$target, " ", sep="")

nodes <- data.frame(name=c(as.character(datosankey$source), as.character(datosankey$target)) %>% unique())
datosankey$IDsource=match(datosankey$source, nodes$name)-1
datosankey$IDtarget=match(datosankey$target, nodes$name)-1

sankeyNetwork(Links = datosankey, Nodes = nodes,
Source = "IDsource", Target = "IDtarget",
Value = "value", NodeID = "name",
sinksRight=FALSE, nodeWidth=60,
fontSize=9,
nodePadding=7)->sankey
sankey

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.

You could use htmlwidgets::onRender() to inject some JavaScript to add the value to the node label...

library(dplyr)
library(networkD3)
library(htmlwidgets)
#> 
#> Attaching package: 'htmlwidgets'
#> The following object is masked from 'package:networkD3':
#> 
#>     JS

datosankey <-read.csv("https://docs.google.com/spreadsheets/d/e/2PACX-1vS45SekhiRvzq8PXATIpG9HjwqNBloVGBuey4Zmf7eM0CPTFKGeT7ZnB_KaSKml_gPF5gzt5nHFbQUs/pub?output=csv")

datosankey$target <- paste(datosankey$target, " ", sep = "")

nodes <- data.frame(name = unique(c(datosankey$source, datosankey$target)))

datosankey$IDsource = match(datosankey$source, nodes$name) - 1
datosankey$IDtarget = match(datosankey$target, nodes$name) - 1

sankey <-
  sankeyNetwork(
    Links = datosankey,
    Nodes = nodes,
    Source = "IDsource",
    Target = "IDtarget",
    Value = "value",
    NodeID = "name",
    sinksRight = FALSE,
    nodeWidth = 60,
    fontSize = 9,
    nodePadding = 7
  )

onRender(
  x = sankey,
  jsCode = '
    function(el, x){
      d3.select(el).selectAll(".node text")
        .text(d => d.name + " (" + d3.format("(.0f")(d.value) + ")");
    }
  '
)