vue d2b sunburst from R
This visualization shows an interactive sunburst chart built with the d2b charting library wrapped in a Vue.js component, with data generated and processed in R. The hierarchical data is created using R's `treemap` package and transformed to JSON via `d3r::d3_nest`, then passed to a Vue app that renders the sunburst using the `vue-d2b` component. The chart displays hierarchical categories (index1, index2, index3) as nested rings, with each arc colored according to a treemap-derived color scheme. A standout feature is the separate breadcrumb component that shows the current selection path. The visualization is interactive: it responds to window resizing, and users can inspect or modify the underlying data in the browser console to see the sunburst update dynamically. The code integrates R, Vue, and the d2b library via htmlwidgets-like scripting, demonstrating a full pipeline from hierarchical data to an interactive web visualization.
AI-generated descriptionNew Sunburst in Town
There is a nue sunburst in town, and it is absolutely beautiful. The new charting library d2b provides us with this delightful sunburst. d2b also plays nicely with vuejs, so of course we will use that version in this quick example from R.
look at that breadcrumb! - it's a separate component for use anywhere
check out resize - dang!!
jump in the console and change the data - pop!!
Code
library(vueR)
library(d3r)
library(treemap)
library(htmltools)
hier_dat <- treemap::random.hierarchical.data()
hier_json <- d3r::d3_nest(
hier_dat,
value_cols = "x"
)
library(dplyr)
hier_json <- hier_dat %>%
treemap(index = c("index1", "index2", "index3"), vSize = "x") %>%
{.$tm} %>%
select(index1:index3,vSize, color) %>%
rename(size = vSize) %>%
d3_nest(value_cols = c("size", "color"))
browsable(tagList(
html_dependency_vue(),
tags$script(src = "https://unpkg.com/d3"),
tags$script(src = "https://unpkg.com/d2b@0.5.1/build/d2b.js"),
tags$script(src = "https://unpkg.com/vue-d2b"),
tags$div(
id = "app",
style = "height:400px",
tag(
"sunburst-chart",
list(":data" = "sunburstChartData", ":config" = "sunburstChartConfig")
)),
tags$script(HTML(
sprintf(
"
var app = new Vue({
el: '#app',
components: {
'sunburst-chart': vued2b.ChartSunburst
},
data: {
sunburstChartData: %s,
sunburstChartConfig: function(chart) {
var color = d3.scaleOrdinal(d3.schemeCategory20c);
chart.label(function(d){return d.name});
//chart.sunburst().size(function(d){return d.x});
//chart.color(function(d){return color(d.name);})
chart.color(function(d){return typeof(d.color) === 'undefined' ? '#BBB' : d.color; })
}
}
})
",
hier_json
)
))
))
Thanks
Thanks to the d2b author Kevin Warne for this fine piece of craftmanship and his pleasant responsiveness (see issue).
forked from <a href='http://bl.ocks.org/timelyportfolio/'>timelyportfolio</a>'s block: <a href='http://bl.ocks.org/timelyportfolio/be8412a07b00de20e725e3757605c63f'>vue d2b sunburst from R</a>