The Name Leslie
df <-
babynames %>%
filter(name == "Leslie")
accumulate_by <- function(dat, var) {
var <- lazyeval::f_eval(var, dat)
lvls <- plotly:::getLevels(var)
dats <-
seq_along(lvls) %>%
purrr::map_dfr(function(x) {
cbind(dat[var %in% lvls[seq(1, x)], ], frame = lvls[[x]])
})
}
df <- df %>%
accumulate_by(~ year)
plot_ly(
data = df %>% filter(sex == "F"),
type = "scatter",
mode = "lines",
x = ~ year,
y = ~ prop,
text = ~ sex,
frame = ~frame,
name = "female"
) %>%
add_trace(
data = df %>% filter(sex == "M"),
name = "male"
) %>%
animation_opts(
frame = 20,
transition = 0,
redraw = FALSE
) %>%
animation_slider(
currentvalue = list(
prefix = "Year "
)
) %>%
layout(
xaxis = list(title = NULL),
yaxis = list(title = "proportion of sex with name")
) %>%
config(
displayModeBar = FALSE
)