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

Possible to change color or opacity between frames in Plotly animation?

Using the Plotly animation example, this works to modify the opacity overall

library(plotly)
library(gapminder)

gap <- gapminder
gap$opa <- 0.5

p <- gap %>%
  plot_ly(
    x = ~gdpPercap, 
    y = ~lifeExp, 
    size = ~pop, 
    color = ~continent, 
    frame = ~year, 
    opacity = ~opa,
    text = ~country, 
    hoverinfo = "text",
    type = 'scatter',
    mode = 'markers'
  ) %>%
  layout(
    xaxis = list(
      type = "log"
    )
  )

p

But what I would like to do is have the opacity change (or color) between frames so that I can hide data points before a certain frame but this doesn’t work:

library(plotly)
library(gapminder)

gap <- gapminder
gap$opa <- ifelse(gap$year < 1970, 0, 1)

p <- gap %>%
  plot_ly(
    x = ~gdpPercap, 
    y = ~lifeExp, 
    size = ~pop, 
    color = ~continent, 
    frame = ~year, 
    opacity = ~opa,
    text = ~country, 
    hoverinfo = "text",
    type = 'scatter',
    mode = 'markers'
  ) %>%
  layout(
    xaxis = list(
      type = "log"
    )
  )

p

Thanks!

1 Like

Did you find out how to change the color between frames?