# Load the library
library(tidyverse)
library(geojsonio)
library(sf)
# Set the fonts
library(showtext)
library(sysfonts)
library(extrafont)
::showtext_auto()
showtext::showtext_opts(dpi=320)
showtextfont_add_google(name="Island Moments",
family="Island Moments")
<- read_csv("~/Documents/R/R_general_resources/30DayMapChallenge/2022/day14_hexagons/data/covid_cases_by_County.csv")
us_covid
<- us_covid%>%
covid_data select(county,
county_fips,
state,
county_population,%>%
covid_cases_per_100k) distinct()
# source: https://walker-data.com/tidycensus/reference/get_decennial.html
library(tidycensus)
options(tigris_use_cache = TRUE)
<- get_acs(geography = "county",
tarr variables = "B19013_001",
geometry = TRUE,
year = 2020)
# save(tarr,file="data/tarr.RData")
load("data/tarr.RData")
%>%count(NAME)
tarr
<- tarr%>%
tarr2 separate(NAME,into=c("county","name"),remove=F,sep=",")
<- tarr2%>%
coords st_centroid() %>%
st_coordinates()
# check the dimensions
%>%dim
tarr2%>%dim
coords%>%dim
covid_data
<- cbind(tarr2,coords)
full
<- covid_data%>%
df left_join(full,by=c("county"="county")) %>%
distinct()%>%
mutate(county=as.factor(county))
<- covid_data%>%
df2 left_join(tarr2,by=c("county"="county")) %>%
distinct()
<- df2%>%
df3 st_as_sf()
%>%DataExplorer::profile_missing()
df# A tibble: 12 × 3
# feature num_missing pct_missing
# <fct> <int> <dbl>
# 1 county 0 0
# 2 county_fips 0 0
# 3 state 0 0
# 4 county_population 1 0.00000180
# 5 covid_cases_per_100k 0 0
# 6 GEOID 4911 0.00884
# 7 NAME 4911 0.00884
# 8 name 4911 0.00884
# 9 variable 4911 0.00884
# 10 estimate 4987 0.00898
# 11 moe 4987 0.00898
# 12 geometry 0 0
ggplot()+
# geom_point(data= df, aes(X,Y),inherit.aes = F)+
stat_summary_hex(data= df, aes(x=X,y=Y,z=covid_cases_per_100k),
linewidth=0.01,
inherit.aes = F)+
geom_sf(data=tarr2,aes(geometry=geometry),
fill=NA,
linewidth=0.05)+
coord_sf(xlim = c(-125,-68),ylim = c(20,50))+
scale_fill_viridis_c(option = "H")+
labs(title="United States of America",
subtitle = "Covid Cases per 100k",
fill="Value",
caption="#30DayMapChallenge 2022 Day 14: Hexagons\nDataSource: CDC Covid data & Census data from {tidycensus}\nMap: Federica Gazzelloni (@fgazzelloni)")+
::theme_map()+
ggthemestheme(plot.caption = element_text(lineheight = 1.5,size=5),
plot.title = element_text(size=14),
legend.background = element_blank())
ggsave("day14_hexagons.png",
bg="grey90",
width=6.78,
height = 5.78,
dpi=200)
Overview
Hexagons
to style a US map of Covid19 cases
, data is from the CDC and the {tidycensus}
package.