plotly / plotly.R

An interactive graphing library for R
https://plotly-r.com
Other
2.55k stars 623 forks source link

Handling missing rows and NAs with animated chart #1615

Open enuma-elis opened 5 years ago

enuma-elis commented 5 years ago

Hi. I found out that when using animated plotly chart you need to have the same number of observations for each of your factors. Meaning -> one missing observation results in whole trace being discarded for entire duration of the animated chart. That is especially a problem when you use time-series data and some of your traces start later, and/or end sooner than others. In addition even when I manually recreate the missing row with NAs, the trace containing these NAs cannot be graphed. Is there any workaround beside of imputing null values for the missings? Thanks!

Example:

library(gapminder)
library(plotly)
library(dplyr)

#working example with no missings
gapminder %>% 
  group_by(year, continent) %>% 
  summarise(pop = mean(pop), gdpPercap = mean(gdpPercap), lifeExp = mean(lifeExp)) %>%
  plot_ly( x = ~gdpPercap, 
           y = ~lifeExp, 
           size = ~pop, 
           color = ~continent, 
           frame = ~year, 
           text = ~continent, 
           hoverinfo = "text",
           type = 'scatter',
           mode = 'markers')

#filtering one row results in missing Africa trace for entirety of the plot

gapminder %>% 
  group_by(year, continent) %>% 
  summarise(pop = mean(pop), gdpPercap = mean(gdpPercap), lifeExp = mean(lifeExp)) %>%
  filter(gdpPercap > 1253) %>% 
  plot_ly( x = ~gdpPercap, 
           y = ~lifeExp, 
           size = ~pop, 
           color = ~continent, 
           frame = ~year, 
           text = ~continent, 
           hoverinfo = "text",
           type = 'scatter',
           mode = 'markers')
pavelgold commented 4 years ago

Hi guys, I run with the same issue. Any updates?