Testing d3

Steve Simon

2022-06-01

I am interested in developing some visualization applications using d3. You can run d3 from within R Markdown.

This is an example that I borrowed from Section 15.5 of R Markdown Cookbook by Yihui Xie, Christophe Dervieus, and Emily Riederer.

First, load the package r2d3 to set up the d3 engine for knitr automatically:

library(r2d3)
## Warning: package 'r2d3' was built under R version 4.1.3
## r2d3 should be run under RStudio v1.2 or higher. Please update at:
## https://www.rstudio.com/products/rstudio/download/

Now we can generate data in R, pass it to D3, and draw the chart:

svg.selectAll('rect')
  .data(data)
  .enter()
    .append('rect')
      .attr('width', function(d) { return d * 672; })
      .attr('height', '10px')
      .attr('y', function(d, i) { return i * 16; })
      .attr('fill', options.color);