Chapter 10 Animations
10.1 Intro to animations
10.1.1 Basic Example
library(plotly)
df <- data.frame(
x = c(1,2,1),
y = c(1,2,1),
f = c(1,2,3)
)
fig <- df %>%
plot_ly(
x = ~x,
y = ~y,
frame = ~f,
type = 'scatter',
mode = 'markers',
showlegend = F
)
fig10.1.2 Mulitple Trace Animations
library(plotly)
library(gapminder)
df <- gapminder
fig <- df %>%
plot_ly(
x = ~gdpPercap,
y = ~lifeExp,
size = ~pop,
color = ~continent,
frame = ~year,
text = ~country,
hoverinfo = "text",
type = 'scatter',
mode = 'markers'
)
fig <- fig %>% layout(
xaxis = list(
type = "log"
)
)
fig10.2 Cumulative animations
10.2.1 Cumulative Lines Animation
library(plotly)
accumulate_by <- function(dat, var) {
var <- lazyeval::f_eval(var, dat)
lvls <- plotly:::getLevels(var)
dats <- lapply(seq_along(lvls), function(x) {
cbind(dat[var %in% lvls[seq(1, x)], ], frame = lvls[[x]])
})
dplyr::bind_rows(dats)
}
df <- txhousing
fig <- df %>%
filter(year > 2005, city %in% c("Abilene", "Bay Area"))
fig <- fig %>% accumulate_by(~date)
fig <- fig %>%
plot_ly(
x = ~date,
y = ~median,
split = ~city,
frame = ~frame,
type = 'scatter',
mode = 'lines',
line = list(simplyfy = F)
)
fig <- fig %>% layout(
xaxis = list(
title = "Date",
zeroline = F
),
yaxis = list(
title = "Median",
zeroline = F
)
)
fig <- fig %>% animation_opts(
frame = 100,
transition = 0,
redraw = FALSE
)
fig <- fig %>% animation_slider(
hide = T
)
fig <- fig %>% animation_button(
x = 1, xanchor = "right", y = 0, yanchor = "bottom"
)
fig10.2.2 Filled-Area Animation
library(plotly)
library(quantmod)
getSymbols("AAPL",src='yahoo')
df <- data.frame(Date=index(AAPL),coredata(AAPL))
df <- tail(df, 30)
df$ID <- seq.int(nrow(df))
accumulate_by <- function(dat, var) {
var <- lazyeval::f_eval(var, dat)
lvls <- plotly:::getLevels(var)
dats <- lapply(seq_along(lvls), function(x) {
cbind(dat[var %in% lvls[seq(1, x)], ], frame = lvls[[x]])
})
dplyr::bind_rows(dats)
}
df <- df %>% accumulate_by(~ID)
fig <- df %>% plot_ly(
x = ~ID,
y = ~AAPL.Close,
frame = ~frame,
type = 'scatter',
mode = 'lines',
fill = 'tozeroy',
fillcolor='rgba(114, 186, 59, 0.5)',
line = list(color = 'rgb(114, 186, 59)'),
text = ~paste("Day: ", ID, "<br>Close: $", AAPL.Close),
hoverinfo = 'text'
)
fig <- fig %>% layout(
title = "AAPL: Last 30 days",
yaxis = list(
title = "Close",
range = c(0,250),
zeroline = F,
tickprefix = "$"
),
xaxis = list(
title = "Day",
range = c(0,30),
zeroline = F,
showgrid = F
)
)
fig <- fig %>% animation_opts(
frame = 100,
transition = 0,
redraw = FALSE
)
fig <- fig %>% animation_slider(
currentvalue = list(
prefix = "Day "
)
)
fig## [1] "AAPL"