gpt4 book ai didi

r - 使用带有 transition_time 的 geom_map 得到错误 : Error in insert_points(polygon$x, polygon$y, splits, n)

转载 作者:行者123 更新时间:2023-12-05 04:40:59 24 4
gpt4 key购买 nike

我正在尝试展示纽约州 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 创建于 2021-11-30 (v2.0.1)

关于r - 使用带有 transition_time 的 geom_map 得到错误 : Error in insert_points(polygon$x, polygon$y, splits, n),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70163323/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com