我試圖展示紐約州 COVID 病例的增長情況
這段代碼得到了我想要的情節,但沒有影片或時間方面。
完整錯誤:
insert_points(polygon$x,polygon$y,splits,n) 中的錯誤:
與請求的型別不兼容:[type=NULL; 目標=雙]。
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)
uj5u.com熱心網友回復:
您需要告訴 {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 包(v2.0.1)于 2021 年 11 月 30 日創建
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/371951.html
上一篇:將點分配到x軸上的特定范圍
