All examples By author By category About

timelyportfolio

sunburst R htmlwidget | % for all levels

uploaded with blockbuilder.org

Sunburst with % for each level

This was inspired by an email request. The result is by no means polished, but I hope this example might demonstrate more advanced usage of the explanation argument in the R htmlwidget sunburstR.

R Code to Replicate

library(sunburstR)

# read in sample visit-sequences.csv data provided in source
#   https://gist.github.com/kerryrodden/7090426#file-visit-sequences-csv
sequences <- read.csv(
  system.file("examples/visit-sequences.csv",package="sunburstR")
  ,header = FALSE
  ,stringsAsFactors = FALSE
)

sunburst(
  sequences,
  explanation = htmlwidgets::JS(
    '
function(d){
var explanation = [];
var parent = d.parent;
if(d.depth > 1){
  while(parent){
    if(parent.parent){
      explanation.push(
      (100*d.value/parent.value).toPrecision(3) + "%"
      );
    }
    parent = parent.parent;
  };
}
explanation.push((100*d.value/this).toPrecision(3) + "%");
return explanation.join("<br/>");
}'
  )
)