【问题标题】:gganimate error with long-format data and plot_usmap长格式数据和 plot_usmap 的 gganimate 错误
【发布时间】:2020-11-11 12:13:01
【问题描述】:

我正在尝试使用 gganimate 为地图制作动画,但遇到以下错误:

Error in seq.default(range[1], range[2], length.out = nframes) : 
'from' must be a finite number

这似乎通常发生在用于transition_time() 的变量不是数字或数据格式错误的情况下,但我不知道我的情况如何;我的数据是正确的长格式,transition_time() 的变量是数字并且总是有限的。

这是plot_usmap() 的特定问题吗?我错过了什么明显的东西吗?

我的数据在GitHub。代码如下:

library("usmap")
library("ggplot2")
library("gganimate")

load("helpdata.Rda")

check = plot_usmap(data = df_map, values = "frac_cov") +
  scale_fill_stepsn(breaks=c(-100, 0, 0.1, 0.2, 0.3, 0.4, 0.5,
                             0.6, 0.7, 0.8, 0.9, 1, 100),
                    colors=c("red", "gray100", "gray90", "gray80", "gray70", "gray60",
                             "gray50", "gray40", "gray30", "gray20", "gray10", "red"),
                    na.value="white") +
  labs(fill = "", title = "Fraction of excess deaths\nattributed to COVID-19") +
  theme(legend.position = "right", plot.title = element_text(hjust = 0.5)) +
  transition_time(month)

anim <- animate(check, nframes=9, fps=1)

非常感谢!

【问题讨论】:

    标签: r ggplot2 gganimate


    【解决方案1】:

    您的数据集有 50 个唯一的 fips 值。 usmap 包中的地图数据带有 51。当plot_usmap 创建 ggplot 图层时,它会通过 fips 代码将您的数据集与地图数据合并,这会将 NA 值添加到月份列,因为其中一个 fips 代码具有不匹配。

    transition_time 对此大惊小怪,而 transition_state 则没有。我对 gganimate 包不够熟悉,无法从理论上解释为什么会这样。)

    在绘图之前将以下内容添加到您的数据集中。它应该可以解决问题:

    df_map <- rbind(df_map,
                    data.frame(month = sort(unique(df_map$month)),
                               frac_cov = 0,
                               fips = 11)) # the missing fips value
    

    运行问题中显示的相同动画代码后的结果:

    【讨论】:

    • 为了后代的缘故——我还必须添加 library("transformr") 才能让动画运行,而我的动画不像 Z.Lin 的例子那样流畅——可能不得不弄乱转换器包来得到那个(?)。
    • 您可能希望检查您的 gganimate 软件包版本。较新的不应该要求加载转换器,并且过渡之间的缓动也应该默认是线性平滑的。
    猜你喜欢
    • 2019-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-20
    • 1970-01-01
    • 1970-01-01
    • 2016-08-13
    • 1970-01-01
    相关资源
    最近更新 更多