Search code examples
rhighchartsr-highcharter

set font size of sankey chart in HIghcharter for R


I have a cool sankey chart done in Hichcharts using R. The ending node has a very small font size to my liking so i want to know how to make it bigger (and the container must be wider too).

highchart() %>%
  hc_chart(type = 'sankey') %>%
  hc_add_series(
      data = list(
        list(from = 'FOOD', to = "M", weight = 10),
        list(from = 'CARS', to = "M",  weight = 50),
        list(from = 'READING', to = "M",  weight = 45),list(from = 'FOOD', to = "F", weight = 100),
        list(from = 'CARS', to = "F",  weight = 56),
        list(from = 'READING', to = "F",  weight = 25))
      )

enter image description here


Solution

  • You can increase the node width using series.nodeWidth property, but it works only for all nodes. If you want to set it only for a specific node, you would have to change the Highcharts core code.

    The font size you can change in the dataLabels.nodeFormatter. Here is a pure JavaScript example: https://jsfiddle.net/BlackLabel/9tL2zogq

    nodeWidth: 50,
        dataLabels: {
          nodeFormatter() {
            if (this.key === 'M' || this.key === 'F') {
              return '<span style="font-size: 22px;">' + this.key + '</span>'
            } else {
              return this.key
            }
          }
        },
    

    Let me know if you have any problems with rewriting it to R, I will edit my answer then. You can inject the JS function into R using JS() method.