Plotly:使用动画图表处理缺失值

Plotly: handling missing values with animated chart

我发现在使用动画图表时,您需要对每个因素进行相同数量的观察。含义 -> 一个缺失的观察结果导致整个轨迹在动画图表的整个持续时间内被丢弃。当您使用时间序列数据并且您的某些跟踪开始较晚或结束较早时,这尤其是一个问题。除了为缺失值输入空值之外,是否有任何解决方法?谢谢!

来自 rstudio community

的交叉发布

示例:

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')

似乎没有直接的方法可以解决这个问题。间接地,可以通过使用 ggplot + ggplotly 而不是 plotly () 来解决数据框中 NA 的问题。此外,当按照我的示例存在不完整的数据集,而不是某些行中的 NA 时,可以使用 tidyverse 包中的完整函数来解决。

查看解决方案:

p <- gapminder %>%
group_by(year, continent) %>%
summarise(pop = mean(pop), gdpPercap = mean(gdpPercap), lifeExp = mean(lifeExp)) %>%
filter(gdpPercap > 1253) %>%
complete(continent,year) %>%
ggplot(aes(gdpPercap, lifeExp, color = continent)) +
geom_point(aes(frame = year)) + theme_bw()

ggplotly(p)

也就是说,我不喜欢在生产中使用变通方法,所以请随时告诉我有关 plotly animate 函数的开发情况。