Using geom_map with transition_time gets Error: Error in insert_points(polygon$x, polygon$y, splits, n)

Using geom_map with transition_time gets Error: Error in insert_points(polygon$x, polygon$y, splits, n)

我正在尝试展示纽约州 COVID 病例的增长情况

这段代码得到了我想要的情节,但没有动画或时间方面。

完整错误:

Error in insert_points(polygon$x, polygon$y, splits, n):
Not compatible with requested type: [type=NULL; target=double].

library(ggplot2)
library(gganimate)
library(transformer)
library(tidyverse)
county_map = map_data("county", region = "New York")

county_map$region = county_map$subregion

covidCounties = read.csv("https://raw.githubusercontent.com/nytimes/covid-19-data/master/us-counties.csv", header = T)
covidCounties = covidCounties %>%
  mutate(date = as.Date(date)) %>%
  filter(state == "New York") %>%
  arrange(date)%>%
  group_by(county) %>%
  mutate(county = tolower(county)) %>%
  mutate(newCases = diff(c(0, cases))) %>%
  mutate(newDeaths = diff(c(0, deaths))) %>%
  ungroup() %>%
  select(date, state, county, cases, newCases, deaths)

covidCountyMap = covidCounties %>%
  ggplot(aes(
    map_id = county,
    fill = newCases,
    group = county
  ))+
  geom_map(
    map = county_map,
    color = "black"
  )+
  expand_limits(x = county_map$long, y = county_map$lat)+
  scale_fill_gradientn(colors = c("green", "yellow", "red"), breaks = c(0, 100, 500))+
  labs(
    title = "New cases over time in New York State",
    subtitle = "{frame_time}"
  )

covidCountyMap

covidCountyMap+
  transition_time(date)

您需要告诉 {gganimate} 哪些多边形可以相互过渡。它无法为您猜测。换句话说,您需要为每个过渡状态(按日期表示每个县)添加一个组标识符。

我只过滤到一个状态,因为整个数据的 reprex 一直在崩溃。为了更好地表示数据范围,我已将您的计数转换为对数刻度。 (有一些负值,因此警告)

library(tidyverse)
library(gganimate)

county_map = map_data("county", region = "New York")

county_map$region = county_map$subregion

## I'd advise to create a separate data frame for your raw data, and not overwrite it
covidCounties_raw = read.csv("https://raw.githubusercontent.com/nytimes/covid-19-data/master/us-counties.csv", header = T)

covidCounties <- covidCounties_raw %>%
  mutate(date = as.Date(date)) %>%
  filter(state == "New York") %>%
  arrange(date) %>%
  group_by(county) %>%
  mutate(county = tolower(county)) %>%
  mutate(newCases = diff(c(0, cases))) %>%
  mutate(newDeaths = diff(c(0, deaths))) %>%
  ungroup() %>%
  select(date, state, county, cases, newCases, deaths) %>%
  ## this is the main trick
  group_by(date, county) %>%
  mutate(id = cur_group_id()) %>%
  ungroup() %>%
  ## I'm filtering for only one county because the reprex took too long with the entire data
  filter(county == "nassau")

covidCountyMap <- covidCounties %>%
  ggplot(aes(
    map_id = county,
    fill = newCases,
## use the group identifier for your grouping
    group = id
  )) +
  geom_map(
    map = county_map,
    color = "black"
  ) +
  expand_limits(x = county_map$long, y = county_map$lat) +
  scale_fill_gradientn(colors = c("green", "yellow", "red"),
## log transformed scale
                       trans = "log") +
  labs(
    title = "New cases over time in New York State",
    subtitle = "{frame_time}"
  )

anim <- covidCountyMap +
  transition_time(date)

## have slightly reduced the frame rate to make it slightly faster
animate(anim, fps = 5, nframes = 50)
#> Warning: Transformation introduced infinite values in discrete y-axis

reprex package (v2.0.1)

于 2021-11-30 创建