---
title: "Sunburst 2.0.0"
author: "Kent Russell"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Sunburst 2.0.0}
  %\VignetteEncoding{UTF-8}
  %\VignetteEngine{knitr::rmarkdown}
editor_options: 
  chunk_output_type: console
---

```{r setup, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
```

`sunburstR` goes `2.0.0` with some new features and bug fixes motivated by the feedback and discussion in issues [60](https://github.com/timelyportfolio/sunburstR/issues/60) and [61](https://github.com/timelyportfolio/sunburstR/issues/61).  I thought a vignette would be a good way to demonstrate.

## Setup and Data

Let's get started by loading some packages and making some simple data to use throughout the post.

```{r}
library(sunburstR)
packageVersion("sunburstR")
```

Simple data should suffice for these examples.

```{r}
library(htmltools)
library(d3r)

dat <- data.frame(
  level1 = rep(c("a", "b"), each=3),
  level2 = paste0(rep(c("a", "b"), each=3), 1:3),
  size = c(10,5,2,3,8,6),
  stringsAsFactors = FALSE
)

knitr::kable(dat)
```

[`d3r`](https://github.com/timelyportfolio/d3r) will help us build our hierarchy.

```{r}
library(d3r)
tree <- d3_nest(dat, value_cols = "size")
tree
```

## `legend = FALSE`

Often the legend in the sunburst becomes useless with lots of nodes and multiple levels.  While a hack could turn it off, I don't like having to ask users to resort to hacks, so now the argument `legend = FALSE` will turn it off.

```{r}
sb1 <- sunburst(tree, width="100%", height=400)
sb2 <- sunburst(
  tree,
  legend = FALSE,
  width = "100%",
  height = 400
)

# do side-by-side for comparison
div(
  style="display: flex; align-items:center;",
  div(style="width:50%; border:1px solid #ccc;", sb1),
  div(style="width:50%; border:1px solid #ccc;", sb2)
)
```

## `d2b` Sunburst

Kevin Warne has built an incredible sunburst in `d2b`, so I just had to add it to `sunburstR`.  I think Kevin's work definitely deserves a star on [Github](https://github.com/d2bjs), and with only 32, I encourage a quick detour to share the love.

```{r}
sb3 <- sund2b(tree, width="100%")

div(
  style="display: flex; align-items:center;",
  sb3
)
```

## Sum Nodes

I love `treemap`, and I enjoy using the results from `treemap` in a sunburst.  However, recent changes resulted in double-counted sums as discussed in this [issue](https://github.com/timelyportfolio/sunburstR/issues/62).  This double counting happens when the tree is pre-summed.  I added a `sumNodes = FALSE` argument to disable the auto-sum.

## Bug Fix

This is hard to see, but I discovered that the breadcrumbs were duplicated on each resize.  This [commit](https://github.com/timelyportfolio/sunburstR/commit/fcb11b002456f2e522b0644d7df09a19332f7c96) shows the bug fix if anyone is interested.

## Ideas and Feedback

Feedback and ideas led to these changes and improvements.  Please keep them coming.
