【问题标题】:Plotly: handling missing values with animated chartPlotly:用动画图表处理缺失值
【发布时间】:2020-01-13 00:50:58
【问题描述】:

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

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

【问题讨论】:

标签: r plotly r-plotly


【解决方案1】:

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

查看解决方案:

p %
group_by(年,大陆) %>%
总结(流行= 平均值(流行),gdpPercap = 平均值(gdpPercap),lifeExp = 平均值(lifeExp))%>%
过滤器(gdpPercap > 1253)%>%
完成(大陆,年份)%>%
ggplot(aes(gdpPercap, lifeExp, color = 大陆)) +
geom_point(aes(frame = year)) + theme_bw()

ggplotly(p)

话虽如此,我不喜欢在生产中使用变通方法,所以请随时告诉我有关 plotly animate 功能的开发。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-09
    • 1970-01-01
    • 2021-11-27
    • 2015-12-30
    • 1970-01-01
    • 2018-09-14
    相关资源
    最近更新 更多