I'm trying to create an animated bubble chart with R and plotly following Sections 14.1 and 16.2 of Carson Sievert's book. However, there seems to be an issue when combining the frame animation option and filter_select. Here's an example with four schools in two states and dummy bivariate data for two types of revenue over five years:
# load libraries
library(plotly)
library(crosstalk)
library(htmlwidgets)
# create data frame
mydf <- data.frame(state = c("al", "al", "al", "al", "al", "al", "al", "al", "al", "al", "ar", "ar", "ar", "ar", "ar", "ar", "ar", "ar", "ar", "ar"), school = c("al schl", "al schl", "al schl", "al schl", "al schl", "al schl2", "al schl2", "al schl2", "al schl2", "al schl2", "ar schl", "ar schl", "ar schl", "ar schl", "ar schl", "ar schl2", "ar schl2", "ar schl2", "ar schl2", "ar schl2"), year = c(0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4), reven = c(1, 3, 5, 7, 9, -1, 1, 3, 5, 7, 3, 4, 5, 6, 7, 2, 3, 4, 5, 6), endow = c(2, 3, 4, 5, 6, -1, 0, 1, 2, 3, 1, 2, 3, 4, 5, 2, 4, 6, 8, 10))
# create a "newdf" SharedData object
newdf <- highlight_key(df)
# plot the animation
plot_ly(newdf, x = ~reven, y = ~endow, type = "scatter", mode = "markers", frame = ~year, color= ~state)
#> Error in if (nrow(df) > 0) as.character(1:nrow(df)) else character(): argument is of length zero
# plot the animation with filter_checkbox for states
bscols( widths = c(3,9), filter_checkbox("workstates", "State Filter", newdf, ~state), plot_ly(newdf, x = ~reven, y = ~endow, type = "scatter", mode = "markers", frame = ~year, color = ~state ))
#> Error in if (nrow(df) > 0) as.character(1:nrow(df)) else character(): argument is of length zero
Created on 2021-07-18 by the reprex package (v2.0.0)
On the last line, here's what happens:
- unfiltered, the animation plays as expected
- with state "al" selected, the plot is limited to the "al" points and the animation plays as expected
- with second state "ar" selected, the plot is limit to the purple "ar" points at first BUT when the animation is played the purple "ar" points are replaced by the green "al" points and the animation plays with the "al" points.
The plot reverts to the first entry in the 'state' list in more complicated examples as well.
Any ideas? Thanks.