Making persistant selections in Plotly's legend for animations


#1

I am trying to use plotly in R with shiny to animate the movement of XY data points (x = Population, y = Cars) over time for a number of global cities. Each city is grouped/colored by the continent that it is in (i.e. all cities in Asia are coloured the same, all cities in Europe are colored the same, etc). Everything at this stage, including the animation already works fine, with the play button and the date slider correctly working to show the movement of these cities’ results over time.

My problem is that my selections/de-selections of specific continents within the chart’s legend doesn’t persist when animating the chart. For example, if I toggle ‘Europe’ in the legend of the plotly chart, as expected all the data points associated with European cities will disappear. However if I then click the play button to run the animation, the European data points reappear again when they should stay hidden. Is there a plotly setting I can change so that my selections within the legend don’t reappear when I animate?

I have attached 3 images below showing this problem.
#1: The starting chart. Everything looks fine. 1
#2: I have deselected ‘Europe’ from the legend and it is now greyed out. All the European data points disappear as expected. No problems here yet. 2
#3: This is where the problem is. Despite ‘Europe’ still being deselected in the legend, the orange/European data points reappear when dragging the date slider to the next day. Is there a way to ensure that they stay hidden? 3

I have copied my R code with shiny below.
Thanks for your help!

server.R

library(“shiny”)
library (“ggplot2”)
library (“plotly”)

setwd (“C:/Desktop”)
file.names <- list.files (pattern = “.csv”, recursive = TRUE)
imported <- sapply (file.names, read.csv, header = TRUE, simplify = FALSE)
names (imported) <- gsub (".csv", “”, names(imported))
names (imported) <- strptime (names(imported), “%Y%m%d”)

for (i in 1:length(imported)) {
imported[[i]]$Date <- names(imported[i])
}

imported <- do.call (rbind, imported)
imported <- as.data.frame (imported)

shinyServer(function(input, output) {
output$chart.animate <- renderPlotly({
chart.xy <- plot_ly()
chart.xy <- add_markers(chart.xy, x = ~Population, y = ~Cars, color = ~Continent, frame = ~Date, ids =~City, data = imported)
chart.xy <- animation_opts(chart.xy, redraw = FALSE)
return (chart.xy)
})
})

ui.R

library(“shiny”)

shinyUI(fluidPage(
mainPanel(
plotlyOutput(‘chart.animate’)
)
))


#2

Here’s an image of one of the .csv data files I am using. All are laid out exactly the same.


#3

Hey Barrio, I might be able to help. I don’t use R, I use plotly with Python3. I can make persistent selections in legend entries through animations by placing all the entries into legend groups with “legendgroup” = “[group name]” in the data dictionaries.

If you put all of the “Asia” series into one legendgroup (and likewise for the other series), your selection / deselection will persist through animations.

I hope that helps.