library(WDI)
library(dplyr)
library(tidyr)
library(xts)
library(dygraphs)
tfr <- WDI(country = "all", indicator = "SP.DYN.TFRT.IN", start = 1960, end = 2012)
df1 <- tfr %>%
rename(tfr = SP.DYN.TFRT.IN) %>%
select(country, year, tfr) %>%
spread(key = country, value = tfr) %>%
mutate(date = as.Date(as.character(year), format = "%Y")) %>%
select(-year)
xtdata <- xts(df1, order.by = df1$date)
xtdata$date <- NULL
d1 <- dygraph(xtdata, main = "Total fertility rate by country, 1960-2012") %>%
dyHighlight(highlightSeriesOpts = list(strokeWidth = 3),
highlightSeriesBackgroundAlpha = 0.2) %>%
dyLegend(show = "onmouseover") %>%
dyAxis("y", label = "Total fertility rate")
d1$x$css = "
.dygraph-legend > span {display:none;}
.dygraph-legend > span.highlight { display: inline; }
"
d1