Black Lives Matter. Please consider donating to Black Girls Code today.

R plotly: add paths only to some frames in an animation

I’ve got an animated scatterplot in which I’d like to connect some points with a path using add_paths. Everything works fine if the data that contains coordinates of the path exists for all animation frames. However, when the coordinates exist only in several frames, the path is not plotted at all.

require(plotly)

df <- data.frame(x = rnorm(100),
                 y = rnorm(100),
                 id = rep(1:5, 20),
                 t = rep(1:20, each=5))

# THIS WORKS
# coordinates for the path joining the points,
# exist in all time frames
dfSub <- subset(df, id %in% c(1, 2))

# THIS DOES NOT WORK
# coordinates for the path joining the points,
# exist only in time frames 5-10
dfSub <- subset(df, id %in% c(1, 2) & t %in% 5:10)

plot_ly(x = ~x,
        y = ~y,
        frame = ~t) %>%
  add_markers(
    data = df,
    ids = ~id,
  ) %>%
  add_paths(
    data = dfSub)