Creating a timeline using R

February 2, 2016
By

(This article was first published on Reimagined Invention, and kindly contributed to R-bloggers)

On Jan, 10th David Bowie left this earthly realm. That day, as I do like his music a lot, I decided to create a timeline and recently I decided to make the code publicly available.

Required packages

You need rCharts whose author is Ramnath Vaidyanathan. The package does not contain an extensive documentation yet but is a great tool.

CSV file

Create a CSV file whose basic columns are

  • Start Date
  • End Date
  • Headline
  • Text
  • Media
  • Media Credit
  • Media Caption
  • Media Thumbnail
  • Type
  • Tag

The only tricky part is that in Excel, Google Sheets or any other cellsheet software both start and end date must be formatted as m/d/yy hh:mm.

Loading packages

setwd("/Users/pacha/bowie-timeline/") #change this to your working folder
library(plyr)
library(RCurl)
library(rCharts)

If rChart is not installed it can be installed using devtools. The package website shows some deprecated parameters and this is what works.

require(devtools)
install_github('ramnathv/rCharts')

Reading data

discography <- read.csv("discography.csv", as.is = TRUE)
discography <- alply(discography, 1, function(x) {
  list(startDate = x$Start.Date, headline = x$Headline, text = x$Text, 
       asset = list(media = x$Media))
})

Creating timeline

tl = Timeline$new()
tl$main(headline = paste0("David Bowie"), type = "default", text = paste0("Discography ", 
        format(as.Date("1967-01-01"), "%Y"), "-", format(as.Date("2016-01-01"), "%Y")), 
        startDate = format(as.Date("1967-01-01"), "%Y"), asset = list(media = "img/cover.jpg"))
names(discography) <- NULL
tl$event(discography)

tl$save("index.html")

Adjustments

Update: Daisuke Ichikawa’s blog has a solution to change the JS path without editing the final html outside RStudio. This are the steps, do them or the timeline won’t work in other computer.

js <- paste(readLines("index.html", warn = F), collapse = "\n")
js <- gsub("/Users/pacha/bowie-timeline/timeline/js/storyjs-embed.js", 
          "timeline/js/storyjs-embed.js", 
          js)
writeLines(js, con = "index.html")

Now, if you open your timeline the dates are displayes as dd/mm/yyyy hh:mm, if you want to show years only then open the html in your editor, look for startDate and the dates will be shown as “1/1/1967 00:00”, you can change it to “1967” without further steps but do the same to any startDate (e.g. use replace “1/1/” for “” in your editor).

Optional step

The final step is to go to the timeline folder that you just copied and get hands into the CSS file, then add the Creative Commons button and favicon, and then you will obtain exactly the same result as me. You can also add Google Analytics code to your timeline as with any html file.

Verification

To avoid many commits to Github, you can run this from terminal

cd /Users/pacha/bowie-timeline
python -m SimpleHTTPServer 8000

and then in your brower go to http://localhost:8000/ to see your changes.

See the full project on Github

Go to the timeline repo available here.

To leave a comment for the author, please follow the link and comment on their blog: Reimagined Invention.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers


Sponsors

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)