Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.