미국 카운티 수준의 데이터를 플로팅하려고하는데 일부 카운티가 표시되지 않는 이유를 알 수 없습니다. 이 장난감 예에서는 캘리포니아 카운티에만 초점을 맞추고 호출에서 필터링 할 때까지 모든 일일 데이터를 남겨 둡니다 ggplot()
(실제 사용 사례는 gganimate를 포함하므로 일일 데이터가 필요합니다).
library(tidyverse)
library(sf)
library(viridis)
library("rio")
# get county geometry
url <- "https://gist.githubusercontent.com/ericpgreen/717596c37478ef894c14b250477fae92/raw/c2cf4b273a2c7f0677f22a37b5e9f7e893204e3b/cali.R"
cali <- rio::import(url)
# get covid data
covid <- read.csv("https://raw.githubusercontent.com/nytimes/covid-19-data/master/us-counties.csv",
stringsAsFactors = FALSE)
# prep covid data
covidPrepped <-
covid %>%
filter(state=="California") %>%
select(date, fips, cases, deaths) %>%
mutate(date = lubridate::ymd(date)) %>%
mutate(fips = stringr::str_pad(fips, width=5, pad="0")) %>%
mutate(month = lubridate::month(date,
label=TRUE,
abbr=TRUE),
day = lubridate::day(date),
monthDay = paste(month, day, sep=" "))
# make sure every county has a row for every day
complete <-
cali %>%
left_join(covidPrepped, by = c("GEOID" = "fips")) %>%
complete(date, GEOID, fill = list(cases = 0)) %>%
select(date, GEOID, cases, monthDay)
# join back to geometry and construct casesPop
pData <-
complete %>%
left_join(select(cali, GEOID, NAME, estimate, geometry),
by = "GEOID") %>%
st_as_sf() %>%
mutate(casesPop = (cases/estimate)*100000) %>%
mutate(casesPop = ifelse(is.na(casesPop), 0, casesPop)) %>%
mutate(group = cut(casesPop,
breaks = c(0, 1, 3, 10, 30, 100,
300, 1000, 3000, 10000,
Inf),
labels = c(0, 1, 3, 10, 30, 100,
300, 1000, 3000, 10000),
include.lowest = TRUE)
) %>%
select(GEOID, geometry, group, monthDay)
# plot
ggplot(pData %>% filter(monthDay=="May 5")) +
geom_sf(aes(fill = group), color = "white", size=.1) +
scale_fill_viridis_d(option = "magma", drop=FALSE) +
coord_sf(crs = 102003) +
theme_minimal() +
theme(legend.position = "top",
legend.box = "horizontal",
legend.title = element_blank(),
legend.justification='left') +
guides(fill = guide_legend(nrow = 1))
누락 된 카운티 :
missing <- pData %>% filter(monthDay=="May 5")
cali$GEOID[!(cali$GEOID %in% test$GEOID)]
#[1] "06035" "06049" "06091" "06105"
이 카운티에는 covid
5 월 5 일에 대한 데이터가 없지만으로 전화하면 문제가 해결 될 것이라고 생각했습니다 complete()
.
complete(date, GEOID, fill = list(cases = 0))
에 complete()
구멍을 남기고 있다는 것을 깨달았 monthDay
으며 나중에 필터링에 사용했습니다. 이러한 NA는 플로팅 할 때 삭제되었습니다.
complete(date, GEOID, fill = list(cases = 0)) %>%
select(date, GEOID, cases, monthDay)
따라서 monthDay
전체 데이터를 기하학 데이터와 결합하여 생성하도록 약간 재구성했습니다 .
library(tidyverse)
library(sf)
library(viridis)
library("rio")
# get county geometry
url <- "https://gist.githubusercontent.com/ericpgreen/717596c37478ef894c14b250477fae92/raw/c2cf4b273a2c7f0677f22a37b5e9f7e893204e3b/cali.R"
cali <- rio::import(url)
# get covid data
covid <- read.csv("https://raw.githubusercontent.com/nytimes/covid-19-data/master/us-counties.csv",
stringsAsFactors = FALSE)
# prep covid data
covidPrepped <-
covid %>%
filter(state=="California") %>%
select(date, fips, cases, deaths) %>%
mutate(date = lubridate::ymd(date)) %>%
mutate(fips = stringr::str_pad(fips, width=5, pad="0"))
# make sure every county has a row for every day
complete <-
cali %>%
left_join(covidPrepped, by = c("GEOID" = "fips")) %>%
complete(GEOID, date, fill = list(cases = 0)) %>%
select(date, GEOID, cases)
# join back to geometry and construct casesPop
pData <-
complete %>%
left_join(select(cali, GEOID, NAME, estimate, geometry),
by = "GEOID") %>%
st_as_sf() %>%
mutate(casesPop = (cases/estimate)*100000) %>%
mutate(casesPop = ifelse(is.na(casesPop), 0, casesPop)) %>%
mutate(group = cut(casesPop,
breaks = c(0, 1, 3, 10, 30, 100,
300, 1000, 3000, 10000,
Inf),
labels = c(0, 1, 3, 10, 30, 100,
300, 1000, 3000, 10000),
include.lowest = TRUE)
) %>%
mutate(month = lubridate::month(date,
label=TRUE,
abbr=TRUE),
day = lubridate::day(date),
monthDay = paste(month, day, sep=" ")) %>%
select(GEOID, geometry, group, monthDay)
# plot
ggplot(pData %>% filter(monthDay=="May 5")) +
geom_sf(aes(fill = group), color = "white", size=.1) +
scale_fill_viridis_d(option = "magma", drop=FALSE) +
coord_sf(crs = 102003) +
theme_minimal() +
theme(legend.position = "top",
legend.box = "horizontal",
legend.title = element_blank(),
legend.justification='left') +
guides(fill = guide_legend(nrow = 1))
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다