简体   繁体   English

长格式数据和 plot_usmap 的 gganimate 错误

[英]gganimate error with long-format data and plot_usmap

I'm trying to animate a map graph using gganimate but am running into the following error:我正在尝试使用gganimate为地图图形设置动画,但gganimate以下错误:

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

It seems like this usually occurs when the variable used for transition_time() isn't numeric or the data's in the wrong form, but I don't know how that could be in my case;当用于transition_time()的变量不是数字或数据格式错误时,似乎通常会发生这种情况,但我不知道在我的情况下如何; my data's in the proper long format and the variable for transition_time() is numeric and always finite.我的数据采用正确的长格式,并且transition_time()的变量是数字且始终是有限的。

Is this a specific issue with plot_usmap() ?这是plot_usmap()的特定问题吗? Am I missing something obvious?我错过了一些明显的东西吗?

My data is on GitHub .我的数据在GitHub 上 Code below:代码如下:

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)

Thanks so much!非常感谢!

Your dataset has 50 unique fips values.您的数据集有 50 个唯一的 fips 值。 The map data in the usmap package comes with 51. When plot_usmap creates the ggplot layers, it merged your dataset with the map data by fips code, which adds on NA values to the month column since one of the fips codes has no match. usmap包中的地图数据带有 51。当plot_usmap创建 ggplot 图层时,它通过 fips 代码将您的数据集与地图数据合并,由于其中一个 fips 代码不匹配,因此将 NA 值添加到月份列中。

( transition_time makes a fuss about this while transition_state doesn't. I'm not familiar enough with the gganimate package to theorize why that's the case.) transition_time对此大惊小怪,而transition_state没有。我对 gganimate 包不够熟悉,无法从理论上解释为什么会这样。)

Add the following to your dataset before plotting.在绘图之前将以下内容添加到您的数据集。 It should solve the problem:它应该解决这个问题:

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

Result after running the same animation code shown in the question:运行问题中显示的相同动画代码后的结果:

地图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM