Quick Introduction to timelineR

﹏ヽ暗。殇╰゛Y 2022-09-21 13:09 110阅读 0赞

Kenton Russell

2015-10-02

timelineR lets us use the new d3.layout.timeline from Elijah Meeks to make interactive d3.js swimlane timelines. If you prefer the more traditional graphics orggplot2 from R, we can also leverage d3.layout.timeline using V8 to produce static plots.

Installation

  1. devtools::install_github("timelyportfolio/timelineR")

Examples

I very much appreciate when authors of R packages and JavaScript libraries provide helpful examples to demonstrate their fine work and provide a foundation for its use. This is definitely the case with Elijah Meeks and d3.layout.timeline. Let’s start our exploration of timelineR by recreating Examples With Dates.

Starting Point

  1. library(timelineR)
  2. # example with dates provided by d3.layout.timeline author
  3. # Elijah Meeks
  4. # http://bl.ocks.org/emeeks/280cb0607c68faf30bb5
  5. t1 <- timeline(
  6. read.csv("http://bl.ocks.org/emeeks/raw/280cb0607c68faf30bb5/wars.csv"),
  7. colorScale = htmlwidgets::JS(
  8. 'd3.scale.ordinal()
  9. .domain(["European","Native","Colonial","Latin America","Internal"])
  10. .range(["#96abb1", "#313746", "#b0909d", "#687a97", "#292014"])
  11. '
  12. ),
  13. color = "sphere",
  14. height = "400",
  15. width = "80%"
  16. )
  17. t1

Add an Axis

  1. add_axis(t1)

1780179018001810182018301840185018601870188018901900

Customize and Style

  1. # use this example as inspiration
  2. # http://jasonheppler.org/projects/war/
  3. library(pipeR)
  4. t1 %>>%
  5. add_axis(tickSize = 400) %>>%
  6. add_tasks(
  7. htmlwidgets::JS(
  8. '
  9. function(){
  10. var svg = d3.select(this.el).select("svg");
  11. svg.selectAll(".timeline-axis > path")
  12. .style("fill","none")
  13. .style("stroke","none");
  14. svg.selectAll(".timeline-axis .tick line")
  15. .style("stroke","gray")
  16. .attr("stroke-dasharray","5 5");
  17. svg.selectAll("rect")
  18. .style("stroke","white")
  19. .attr("rx",4);
  20. }
  21. '
  22. )
  23. )

1780179018001810182018301840185018601870188018901900

Static Plots with ggplot2

In a feat of super-isomorphism, let’s use d3.layout.timeline to calculate the layout and coordinates for a ggplot2 chart.

  1. library(ggplot2)
  2. library(V8)
  3. ctx <- new_context()
  4. # get d3
  5. ctx$source(system.file("htmlwidgets/lib/d3/d3.min.js",package="timelineR"))
  6. ## [1] "true"
  7. # get d3.layout.timeline
  8. ctx$source(system.file("htmlwidgets/lib/d3.layout.timeline/d3.layout.timeline.js",package="timelineR"))
  9. # assign the simple example data to data
  10. ctx$assign("data", read.csv("http://bl.ocks.org/emeeks/raw/d24171dac80dd535521b/int_bands.csv"))
  11. # do the layout and get it in R
  12. invisible(ctx$eval(
  13. '
  14. var timeline = d3.layout.timeline()
  15. .size([1000,300])
  16. .bandStart(function (d) {return d.s})
  17. .bandEnd(function (d) {return d.e})
  18. .dateFormat(function (d) {return parseInt(d)})
  19. timelineBands = timeline(data);
  20. '
  21. ))
  22. # see if it worked
  23. time_bands <- ctx$get("timelineBands")
  24. library(ggplot2)
  25. ggplot(
  26. time_bands,
  27. aes(
  28. xmin = start,
  29. ymin = y,
  30. xmax = end,
  31. ymax = y + dy
  32. )
  33. ) + ylim(300,0) + geom_rect(fill = "#687a97", colour = "white")

![Image 1][]

Thanks

Elijah Meeks

Elijah has made significant contribiutions to the d3.js community. I highly recommend his book D3.js in Action.

Mike Bostock

Mike Bostock has provided us an unbelievable platform to do amazing things withd3.js.

Ramnath Vaidyanathan and RStudio

Ramnath Vaidyanathan and the very skilled, generous folks at RStudio have made it easy with htmlwidgets to seamlessly integrate interactive JavaScript/HTML/CSS in R.

[Image 1]:

发表评论

表情:
评论列表 (有 0 条评论,110人围观)

还没有评论,来说两句吧...

相关阅读