Skip to content

Instantly share code, notes, and snippets.

@walkerke
Created April 17, 2014 18:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save walkerke/11002398 to your computer and use it in GitHub Desktop.
Save walkerke/11002398 to your computer and use it in GitHub Desktop.
library(WDI)
library(rCharts)
library(plyr)
countries <- c("AL", "AT", "BE", "BA", "BG", "HR", "CZ", "DK", "FI", "FR", "DE", "GR",
"HU", "IS", "IE", "IT", "NL", "NO", "PL", "PT", "RO", "RS", "SK", "SI",
"ES", "SE", "CH", "GB")
tfr <- WDI(country = countries, indicator = "SP.DYN.TFRT.IN", start = 1960, end = 2011)
#Clean up the data a bit
tfr <- rename(tfr, replace = c("SP.DYN.TFRT.IN" = "TFR"))
tfr$TFR <- round(tfr$TFR, 2)
# Create the chart
tfrPlot <- nPlot(
TFR ~ year,
data = tfr,
group = "country",
type = "lineChart")
# Add axis labels and format the tooltip
tfrPlot$yAxis(axisLabel = "Total fertility rate", width = 62)
tfrPlot$xAxis(axisLabel = "Year")
# Here, the 'e' parameter allows you to pass other variables to the tooltip, like 'iso2c' in this example.
tfrPlot$chart(tooltipContent = "#! function(key, x, y, e){
return '<h3>' + key + ' (' + e.point.iso2c + ')' + '</h3>' +
'<p>' + y + ' in ' + x + '</p>'
} !#")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment