What is D3.js?

Javascript is a scripting language that is embedded in the HTML to make web pages interactive and dynamics.

Scalable Vector Graphics (SVG) is an image format that is necessary for javascript to bind with its elements, and make them interactive.


Why use D3.js?


The structure of an HTML source code is as follows:

<!DOCTYPE html>
<html>
  <head>
    <title>This is a title</title>
    <style>Styling elements in the html document</style>
    <script>"javascript for adding dynamics to html"</script>
  </head>
  <body>
    <p>Hello world!</p>
    <script>"also add interactivity and dynamics to the visuals that will appear" </script>
  </body>
</html>

2 Methods for creating interactive plots with R and javascript

  1. Use R for data processing and creating a graphic. The we export the graphic in SVG format and bind javascript to the elements we desire. Example of manually binding javascript: (http://vanhumbeecka.github.io/R-and-D3/svg-simple.html)
  2. Use R only for data processing & use javascript for entire visualization process. We get more customization & these can potentially result in some pretty graphics. You can inspect the elements for the exmaples below to see if d3 functions are present.

Here is the vaccine graphic from earlier in the course: http://www.sciencemag.org/news/2017/04/here-s-visual-proof-why-vaccines-do-more-good-harm

Here is another: http://www.nytimes.com/newsgraphics/2014/01/05/poverty-map/index.html

One more interactive plot that doesn’t use d3.js: https://www.nytimes.com/interactive/2017/04/02/technology/uber-drivers-psychological-tricks.html

Both methods require the use of javascript to create these customized plots.


Here’s a gallery of different plots that use the D3.js library: https://github.com/d3/d3/wiki/Gallery


Final words


Some Useful Resources

https://www.w3schools.com/js/ (Javascript tutorials)
https://www.w3schools.com/html/default.asp (HTML tutorials)
https://www.rdocumentation.org/collaborators/name/Mike%20Bostock (Mike Bostock’s packages)
https://github.com/d3 (D3 github repositories) https://github.com/d3/d3/wiki/Tutorials (D3.js tutorials)