I’m trying to make an animation with two traces, including a button that hides or shows the specific trace. The button works and the animation works, however, the button only works on a single frame. As soon as the animation activates it shows both traces again. Is it possible to hide or show the traces in all animation frames?
Example data and code:
# Example data
x <- seq(-2*pi, 2*pi, length.out = 100)
frame <- seq(0, pi/2, length.out = 10)
df <- merge(x = x, y = frame, by = NULL)
names(df)[2] <- "frame"
df$y1 <- sin(df$x + df$frame)
df$y2 <- cos(df$x + df$frame)
# Create the animated plot
p <- plot_ly(df, x = ~df$x) %>%
add_trace(y = ~df$y1
, frame = ~df$frame
, name = "Sin"
, type = 'scatter'
, mode = 'lines'
) %>%
add_trace(y = ~df$y2
, name = "Cos"
, frame = ~df$frame
, type = 'scatter'
, mode = 'lines'
) %>%
layout(
yaxis = list(title = "Y"),
xaxis = list(title = "X"),
updatemenus = list(
list(
y = 0.7,
buttons = list(
list(method = "restyle",
args = list("visible", list(TRUE, TRUE)),
label = "Both"),
list(method = "restyle",
args = list("visible", list(TRUE, FALSE)),
label = "Sin"),
list(method = "restyle",
args = list("visible", list(FALSE, TRUE)),
label = "Cos")))
)
)
p